KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > daemon > impl > util > feeddaemon > Instantiator


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.daemon.impl.util.feeddaemon;
18
19 //jetspeed stuff
20
import org.apache.jetspeed.cache.disk.DiskCacheUtils;
21 import org.apache.jetspeed.cache.disk.JetspeedDiskCache;
22 import org.apache.jetspeed.om.registry.PortletEntry;
23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24 import org.apache.jetspeed.services.logging.JetspeedLogger;
25 import org.apache.jetspeed.services.Registry;
26 import org.apache.jetspeed.services.PortletFactory;
27 import org.apache.jetspeed.services.urlmanager.URLManager;
28 import org.apache.jetspeed.services.urlmanager.URLManagerService;
29 import org.apache.jetspeed.services.urlmanager.URLFetcher;
30 import org.apache.jetspeed.services.resources.JetspeedResources;
31
32 //Java stuff
33
import java.io.IOException JavaDoc;
34
35 /**
36 <p>
37 Given an PortletEntry use the PortletFactory to instantiate this Portlet and
38 then place it in the cache.
39 </p>
40
41 <p>
42 If the URL isn't
43
44 </p>
45
46 @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
47 @version $Id: Instantiator.java,v 1.26 2004/02/23 02:47:27 jford Exp $
48 */

49 public class Instantiator implements Runnable JavaDoc {
50
51     /**
52     The maximum number of seconds to wait before warning that the URL took
53     too long to download
54     */

55     public static final int MAX_WARN_SECONDS = 3;
56     
57     /**
58     Specify the interval to log when Portlets are instantiated
59     */

60     public static final int LOG_INTERVAL = 100;
61     
62     private PortletEntry entry = null;
63     private int id = 0;
64
65     private boolean forcePortet = false;
66     
67     /**
68      * Static initialization of the logger for this class
69      */

70     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(Instantiator.class.getName());
71     
72     /**
73     Create a Instantiator with info on what to instantiate
74     */

75     public Instantiator( PortletEntry entry ) {
76
77         this.entry = entry;
78         
79     }
80     
81     /**
82     @see #Instantiator( PortletEntry )
83     */

84     public Instantiator( int id,
85                          PortletEntry entry ) {
86
87         this(entry);
88         this.id = id;
89         
90     }
91
92     /**
93     Get the url from the net and put it on disk
94     */

95     public void getURL( String JavaDoc url ) throws IOException JavaDoc {
96
97         //if the user wants to download the URL and it isn't in the cache then go for it
98
if ( JetspeedResources.getBoolean( JetspeedResources.CONTENTFEEDS_FETCHALL_KEY ) &&
99              DiskCacheUtils.isCached( url ) == false ) {
100             
101             long download_begin = System.currentTimeMillis();
102             try {
103
104                 //JetspeedDiskCache.getInstance().getEntry( url, true );
105
//SGP
106
JetspeedDiskCache.getInstance().getEntry(
107                                                          url,
108                                                          URLFetcher.fetch(url, true));
109             
110                 long total = ( System.currentTimeMillis() - download_begin ) / 1000;
111             
112                 if ( total >= MAX_WARN_SECONDS ) {
113                     logger.warn( this.getClass().getName() + " The following URL took too long (" +
114                     total +
115                     " second(s)) to download: " + url );
116                 }
117             
118             } catch (IOException JavaDoc e) {
119             
120                 //Not necessary to print a stack trace here because this will
121
//generate too much output
122

123                 logger.error( "The following URL couldn't be downloaded " +
124                            url +
125                            " and took " +
126                            ( System.currentTimeMillis() - download_begin ) / 1000 +
127                            " seconds to download. " );
128                 throw new IOException JavaDoc( e.getMessage() );
129             }
130         
131         }
132         
133     }
134     
135     /**
136     Do work necessary to instantiate the current Entry but only do this if it is
137     NOT already in the cache.
138     */

139     public void run() {
140
141         try {
142             
143             if(this.entry == null)
144                 {
145                     logger.error("Instantiator: Null Entry");
146                     return;
147                 }
148
149             if(this.entry.getURL() == null)
150                 {
151                     logger.error("Instantiator: Null URL");
152                     return;
153                 }
154
155             this.getURL( this.entry.getURL() );
156             
157         } catch ( IOException JavaDoc e ) {
158             //the real IOException is logged in getURL
159
return;
160         } catch ( Throwable JavaDoc t) {
161             //t.printStackTrace();
162
logger.error( "Instantiator: Throwable", t);
163         }
164
165         org.apache.jetspeed.om.registry.Registry registry =
166             Registry.get(Registry.PORTLET);
167
168         try {
169             if(!registry.hasEntry(this.entry.getName()))
170                 {
171                     registry.addEntry( this.entry );
172
173                     if ( JetspeedResources.getBoolean( JetspeedResources.AUTOCREATE_PORTLETS_KEY ) )
174                         {
175                   
176                             PortletFactory.getPortlet( this.entry.getName(), "0" );
177
178                         }
179
180                 }
181                                                      
182         } catch ( Exception JavaDoc e ) {
183             logger.error( "InstantiatorThread: Couldn't create Portlet: ", e );
184
185             //SGP We add the URL to the BadList
186
URLManager.register( this.entry.getURL(), URLManagerService.STATUS_BAD, e.toString() );
187
188             //remove this entry because it threw a PortletException so users
189
//should be prevented from seeing this again.
190
registry.removeEntry( this.entry.getName() );
191         }
192         
193         //log how many portlets we have instantiated.
194
if ( id != 0 &&
195              id % LOG_INTERVAL == 0 ) {
196             logger.info( "Instantiator: instanted " + id + " portlet(s)" );
197         }
198         
199     }
200     
201 }
202
203
Popular Tags