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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.daemon.impl.util.feeddaemon;
1819//jetspeed stuff20import org.apache.jetspeed.cache.disk.DiskCacheUtils;
21import org.apache.jetspeed.cache.disk.JetspeedDiskCache;
22import org.apache.jetspeed.om.registry.PortletEntry;
23import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24import org.apache.jetspeed.services.logging.JetspeedLogger;
25import org.apache.jetspeed.services.Registry;
26import org.apache.jetspeed.services.PortletFactory;
27import org.apache.jetspeed.services.urlmanager.URLManager;
28import org.apache.jetspeed.services.urlmanager.URLManagerService;
29import org.apache.jetspeed.services.urlmanager.URLFetcher;
30import org.apache.jetspeed.services.resources.JetspeedResources;
3132//Java stuff33import java.io.IOException;
3435/***36<p>37Given an PortletEntry use the PortletFactory to instantiate this Portlet and38then place it in the cache. 39</p>4041<p>42If the URL isn't 4344</p>4546@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*/49publicclassInstantiator implements Runnable {
5051/***52 The maximum number of seconds to wait before warning that the URL took53 too long to download54 */55publicstaticfinalint MAX_WARN_SECONDS = 3;
5657/***58 Specify the interval to log when Portlets are instantiated59 */60publicstaticfinalint LOG_INTERVAL = 100;
6162privatePortletEntry entry = null;
63privateint id = 0;
6465privateboolean forcePortet = false;
6667/***68 * Static initialization of the logger for this class69 */70privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(Instantiator.class.getName());
7172/***73 Create a Instantiator with info on what to instantiate74 */75publicInstantiator( PortletEntry entry ) {
7677this.entry = entry;
7879 }
8081/***82 @see #Instantiator( PortletEntry )83 */84publicInstantiator( int id,
85PortletEntry entry ) {
8687this(entry);
88this.id = id;
8990 }
9192/***93 Get the url from the net and put it on disk94 */95publicvoid getURL( String url ) throws IOException {
9697//if the user wants to download the URL and it isn't in the cache then go for it98if ( JetspeedResources.getBoolean( JetspeedResources.CONTENTFEEDS_FETCHALL_KEY ) &&
99 DiskCacheUtils.isCached( url ) == false ) {
100101long download_begin = System.currentTimeMillis();
102try {
103104//JetspeedDiskCache.getInstance().getEntry( url, true );105//SGP106 JetspeedDiskCache.getInstance().getEntry(
107 url,
108 URLFetcher.fetch(url, true));
109110long total = ( System.currentTimeMillis() - download_begin ) / 1000;
111112if ( 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 }
117118 } catch (IOException e) {
119120//Not necessary to print a stack trace here because this will 121//generate too much output122123 logger.error( "The following URL couldn't be downloaded " +
124 url +
125" and took " +
126 ( System.currentTimeMillis() - download_begin ) / 1000 +
127" seconds to download. " );
128thrownew IOException( e.getMessage() );
129 }
130131 }
132133 }
134135/***136 Do work necessary to instantiate the current Entry but only do this if it is 137 NOT already in the cache.138 */139publicvoid run() {
140141try {
142143if(this.entry == null)
144 {
145 logger.error("Instantiator: Null Entry");
146return;
147 }
148149if(this.entry.getURL() == null)
150 {
151 logger.error("Instantiator: Null URL");
152return;
153 }
154155this.getURL( this.entry.getURL() );
156157 } catch ( IOException e ) {
158//the real IOException is logged in getURL159return;
160 } catch ( Throwable t) {
161//t.printStackTrace();162 logger.error( "Instantiator: Throwable", t);
163 }
164165 org.apache.jetspeed.om.registry.Registry registry =
166 Registry.get(Registry.PORTLET);
167168try {
169if(!registry.hasEntry(this.entry.getName()))
170 {
171 registry.addEntry( this.entry );
172173if ( JetspeedResources.getBoolean( JetspeedResources.AUTOCREATE_PORTLETS_KEY ) )
174 {
175176 PortletFactory.getPortlet( this.entry.getName(), "0" );
177178 }
179180 }
181182 } catch ( Exception e ) {
183 logger.error( "InstantiatorThread: Couldn't create Portlet: ", e );
184185//SGP We add the URL to the BadList186 URLManager.register( this.entry.getURL(), URLManagerService.STATUS_BAD, e.toString() );
187188//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 }
192193//log how many portlets we have instantiated.194if ( id != 0 &&
195 id % LOG_INTERVAL == 0 ) {
196 logger.info( "Instantiator: instanted " + id + " portlet(s)" );
197 }
198199 }
200201 }
202