1 16 17 package org.apache.jetspeed.daemon.impl; 18 19 20 import org.apache.jetspeed.daemon.Daemon; 22 import org.apache.jetspeed.daemon.DaemonConfig; 23 import org.apache.jetspeed.daemon.DaemonEntry; 24 import org.apache.jetspeed.daemon.Feed; 25 import org.apache.jetspeed.util.SimpleTransform; 26 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 27 import org.apache.jetspeed.services.logging.JetspeedLogger; 28 import org.apache.jetspeed.services.resources.JetspeedResources; 29 import org.apache.jetspeed.services.registry.FileRegistry; 30 import org.apache.jetspeed.services.registry.RegistryService; 31 32 import org.apache.turbine.services.TurbineServices; 34 35 import java.io.Reader ; 37 import java.util.Vector ; 38 39 63 public class FeedDaemon implements Daemon 64 { 65 public static final String TEMP_FILE_KEY = "FeedDaemon-debug"; 66 67 public static String TEMP_DIRECTORY = 68 JetspeedResources.getString( JetspeedResources.TEMP_DIRECTORY_KEY ); 69 70 private static boolean processed = false; 71 72 private static FeedDaemon instance = null; 73 74 75 76 private int status = Daemon.STATUS_NOT_PROCESSED; 77 private int result = Daemon.RESULT_UNKNOWN; 78 private DaemonConfig config = null; 79 private DaemonEntry entry = null; 80 private boolean initialized = false; 81 82 85 private static int count = 0; 86 87 90 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(FeedDaemon.class.getName()); 91 92 95 public static int getCount() 96 { 97 return FeedDaemon.count; 98 } 99 100 101 104 public static Feed[] getFeeds() 105 { 106 Vector v = JetspeedResources.getVector( "contentfeeds.feed.name" ); 107 Vector found = new Vector (); 108 109 for( int i = 0; i < v.size(); ++i) { 110 String name = (String )v.elementAt(i); 111 112 String description = JetspeedResources.getString( "contentfeeds.feed." + name + ".description" ); 113 114 String url = JetspeedResources.getString( "contentfeeds.feed." + name + ".url" ); 115 116 found.addElement( new Feed( name, 117 description, 118 url ) ); 119 } 120 121 124 Feed[] feeds = new Feed[found.size()]; 125 found.copyInto(feeds); 126 return feeds; 127 128 } 129 130 131 133 public void run() 134 { 135 try 136 { 137 this.setResult( Daemon.RESULT_PROCESSING ); 138 139 logger.info( "Jetspeed: FeedDaemon -- BEGIN -- " ); 140 141 FeedDaemon.count = 0; 142 Feed[] feeds = getFeeds(); 143 144 for (int i = 0; i < feeds.length; ++i ) { 145 146 String url = feeds[i].getURL(); 147 148 String name = "feed_"+feeds[i].getName(); 149 150 Reader transformed; 151 152 try { 153 154 logger.info( "BEGIN FEED -> " + url ); 155 156 transformed = getEntries( url ); 158 159 logger.info("Determining portlets..."); 161 162 FileRegistry registry = (FileRegistry)TurbineServices.getInstance() 163 .getService( RegistryService.SERVICE_NAME ); 164 165 registry.createFragment( name, transformed , true ); 166 167 logger.info( "END FEED -> " + url + " -> SUCCESS"); 168 169 this.setResult( Daemon.RESULT_SUCCESS ); 170 171 } catch ( Exception e ) { 172 error( e, "FeedDaemon: Couldn't process URL: " + url ); 173 174 } catch ( Throwable t ) { 175 error( t, "FeedDaemon: Couldn't process URL: " + url ); 176 } 177 178 } 179 180 } finally { 181 logger.info( "Jetspeed: FeedDaemon -- END -- " ); 182 } 183 184 } 185 186 189 private void error( Throwable t, String message ) 190 { 191 this.setResult( Daemon.RESULT_FAILED ); 192 logger.error( message, t ); 193 } 194 195 197 private void error( String message ) 198 { 199 this.error( null, message ); 200 } 201 202 205 public static final Reader getEntries( String url ) throws Exception 206 { 207 210 String stylesheet = JetspeedResources.getString( JetspeedResources.CONTENTFEEDS_STYLESHEET_URL_KEY ); 211 212 logger.info( "FeedDaemon: transforming url: " + 213 url + 214 " with stylesheet: " + 215 stylesheet ); 216 217 return SimpleTransform.SAXTransform( url, stylesheet, null ); 218 } 219 220 221 222 225 private static String strip(String target) 226 { 227 if ( target.indexOf("<!DOCTYPE") != -1 ) { 228 229 int begin = target.indexOf( "\">" ) + 2; 230 231 target = target.substring( begin, target.length() ); 232 233 } 234 235 return target; 236 } 237 238 239 240 243 public void init( DaemonConfig config, DaemonEntry entry ) 244 { 245 this.config = config; 246 this.entry = entry; 247 } 248 249 251 public DaemonConfig getDaemonConfig() 252 { 253 return this.config; 254 } 255 256 258 public DaemonEntry getDaemonEntry() 259 { 260 return this.entry; 261 } 262 263 270 public int getStatus() 271 { 272 return this.status; 273 } 274 275 282 public void setStatus(int status) 283 { 284 this.status = status; 285 } 286 287 290 public int getResult() 291 { 292 return this.result; 293 } 294 295 298 public void setResult( int result ) 299 { 300 this.result = result; 301 } 302 303 306 public String getMessage() 307 { 308 return "Total number of content feeds found: " + getCount(); 309 } 310 311 } 312 | Popular Tags |