View Javadoc

1   /* $Id: CrawlerImpl.java 5979 2008-08-22 21:21:33Z dbernstein $
2    *
3    * Created on Dec 12, 2005
4    *
5    * Copyright (C) 2005 Internet Archive.
6    *  
7    * This file is part of the Heritrix Cluster Controller (crawler.archive.org).
8    *  
9    * HCC is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser Public License as published by
11   * the Free Software Foundation; either version 2.1 of the License, or
12   * any later version.
13   * 
14   * Heritrix is distributed in the hope that it will be useful, 
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU Lesser Public License
20   * along with Heritrix; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22   */
23  package org.archive.hcc.client;
24  
25  import java.util.Collection;
26  import java.util.LinkedList;
27  import java.util.Set;
28  
29  import javax.management.MBeanServerConnection;
30  import javax.management.ObjectName;
31  import javax.management.openmbean.CompositeData;
32  import javax.management.openmbean.TabularData;
33  
34  /***
35   * 
36   * @author Daniel Bernstein (dbernstein@archive.org)
37   *
38   */
39  public class CrawlerImpl extends ProxyBase implements Crawler {
40      public void startPendingJobQueue() {
41          try {
42              this.connection.invoke(
43                      this.name,
44                      "startCrawling",
45                      new Object[0],
46                      new String[0]);
47          } catch (Exception e) {
48              e.printStackTrace();
49              throw new IllegalStateException(e);
50          }
51      }
52  
53      public void stopPendingJobQueue() {
54          try {
55              this.connection.invoke(
56                      this.name,
57                      "stopCrawling",
58                      new Object[0],
59                      new String[0]);
60          } catch (Exception e) {
61              e.printStackTrace();
62              throw new IllegalStateException(e);
63          }
64  
65      }
66  
67      public CrawlerImpl(ObjectName name, MBeanServerConnection connection) {
68          super(name, connection);
69      }
70  
71      public boolean isCrawling(){
72          try {
73              return this.connection
74                      .getAttribute(this.name, "Status")
75                      .toString()
76                      .contains("isCrawling=true");
77          } catch (Exception e) {
78              throw new IllegalStateException(e);
79          }
80      }
81  
82      
83      public boolean isPendingJobQueueRunning() {
84          try {
85              return this.connection
86                      .getAttribute(this.name, "Status")
87                      .toString()
88                      .contains("isRunning=true");
89          } catch (Exception e) {
90              throw new IllegalStateException(e);
91          }
92      }
93  
94      public String getVersion() {
95          try {
96              return this.connection
97                      .getAttribute(this.name, "Version")
98                      .toString();
99          } catch (Exception e) {
100             throw new IllegalStateException(e);
101         }
102     }
103 
104     public void destroy() {
105         try {
106             this.connection.invoke(
107                     this.name,
108                     "destroy",
109                     new Object[0],
110                     new String[0]);
111         } catch (Exception e) {
112             throw new IllegalStateException(e);
113         }
114     }
115 
116     public String addJob(JobOrder order) {
117         try {
118             return (String) this.connection.invoke(
119                     this.name,
120                     "addJob",
121                     new Object[] { order.getJarFile().getAbsolutePath(),
122                             order.getName(), order.getDescription(), "" },
123                     new String[] { "java.lang.String", "java.lang.String",
124                             "java.lang.String", "java.lang.String" });
125 
126         } catch (Exception e) {
127             throw new IllegalStateException(e);
128         }
129 
130     }
131 
132     public void terminateCurrentJob() {
133         try {
134             this.connection.invoke(
135                     this.name,
136                     "terminateCurrentJob",
137                     new Object[0],
138                     new String[0]);
139         } catch (Exception e) {
140             throw new IllegalStateException(e);
141         }
142 
143     }
144     
145 
146     String getCrawlReport(Long uid) throws ClusterException{
147         return getReport("crawl-report", uid);
148     }
149     
150     String getHostsReport(Long uid) throws ClusterException{
151         return getReport("hosts-report", uid);
152     }
153     
154     String getSourceReport(Long uid) throws ClusterException{
155         return getReport("source-report", uid);
156     }
157 
158     String getSeedsReport(Long uid) throws ClusterException{
159         return getReport("seeds-report",uid);
160     }
161 
162     String getMimeTypesReport(Long uid) throws ClusterException{
163         return getReport("mimetype-report",uid);
164     }
165 
166     
167     /***
168      * 
169      * @param reportName
170      * @param uid
171      * @return
172      * @throws ClusterException
173      */
174     String getReport(String reportName, Long uid) throws ClusterException{
175         
176         try {
177             return (String)this.connection.invoke(
178                                 this.name, 
179                                 "crawlendReport", 
180                                 new Object[]{uid.toString(), reportName}, 
181                                 new String[]{"java.lang.String", "java.lang.String"});
182         } catch (Exception e) {
183             e.printStackTrace();
184             throw new ClusterException(e);
185         } 
186     }
187 
188 
189     public boolean deleteCompletedCrawlJob(CompletedCrawlJob job) throws ClusterException{
190         try {
191             this.connection.invoke(
192                                 this.name, 
193                                 "deleteJob", 
194                                 new Object[]{job.getUid().toString()}, 
195                                 new String[]{"java.lang.String"});
196                                 return true;
197         } catch (Exception e) {
198             e.printStackTrace();
199             throw new ClusterException(e);
200         } 
201     }
202 
203     public boolean deletePendingCrawlJob(PendingCrawlJob job) {
204         //TODO implement this 
205         throw new UnsupportedOperationException("deletePendingCrawlJob not implemented yet!");
206     }
207 
208     public Collection<CompletedCrawlJob> listCompletedCrawlJobs() {
209         Collection<CompletedCrawlJob> completedJobs = new LinkedList<CompletedCrawlJob>();
210 
211         try {
212             
213             TabularData td = (TabularData)this.connection.invoke(
214                     this.name, 
215                     "completedJobs", 
216                     new Object[0], 
217                     new String[0]);
218             
219 
220             if(td != null){
221                 for(CompositeData cd: (Collection<CompositeData>)td.values()){
222                     
223                     CompletedCrawlJobImpl ccj = 
224                         new CompletedCrawlJobImpl(
225                                 new Long((String)cd.get("uid")), 
226                                 (String)cd.get("name"), 
227                                 this, 
228                                 this.connection);
229                     
230                     completedJobs.add(ccj);
231                     
232                 }
233                 
234             }
235             
236             
237         } catch (Exception e) {
238             e.printStackTrace();
239         } 
240         
241         return completedJobs;
242     }
243 
244     public Collection<PendingCrawlJob> listPendingCrawlJobs() {
245         //TODO implement this 
246         throw new UnsupportedOperationException("listPendingCrawlJobs not implemented yet!");
247     }
248 }