1 5 6 package org.joseki.server; 7 8 import java.util.* ; 9 import org.mortbay.jetty.* ; 10 import org.mortbay.util.MultiException; 11 12 import org.mortbay.util.LoggerLogSink; 13 import org.mortbay.util.LogSink; 14 import org.mortbay.util.NullLogSink; 15 import org.mortbay.jetty.servlet.* ; 16 17 import org.joseki.server.http.*; 19 import org.apache.commons.logging.* ; 20 21 import org.apache.commons.logging.impl.Jdk14Logger; 22 24 29 30 31 public class RDFServer 32 { 33 static final Log log = LogFactory.getLog(RDFServer.class.getName()) ; 34 35 public static final String DispatcherName = "JenaRDFServer" ; 36 static int count = 0 ; 37 JosekiWebAPI httpServlet = null ; 38 Server server = null ; 39 WebApplicationContext webAppContextJoseki = null ; 40 boolean earlyInitialize = true ; 41 42 int port = -1 ; 43 44 45 public static final String propertyPort = "jena.rdfserver.port" ; 46 47 49 public static final String propertyModelSet = "jena.rdfserver.modelset" ; 50 51 52 public static final int defaultPort = 2020 ; 53 54 55 public static final String defaultServerBaseURI = "/" ; 56 57 58 public static final String defaultConfigFile = "etc/joseki.n3" ; 59 60 62 public static final String noMapValue = "<<null>>" ; 63 64 65 public RDFServer() { this(defaultConfigFile) ; } 66 67 70 public RDFServer(String configFile) 71 { 72 String tmp = System.getProperty(propertyPort, defaultPort+"") ; 73 int p = Integer.parseInt(tmp) ; 74 init(configFile, defaultPort, defaultServerBaseURI) ; 75 } 76 77 81 public RDFServer(String configFile, int port) { init(configFile, port, defaultServerBaseURI) ; } 82 83 88 public RDFServer(String configFile, int port, String serverBaseURI) 89 { 90 init(configFile, port, serverBaseURI) ; 91 } 92 93 private void init(String configFile, int port, String serverBaseURI) 94 { 95 boolean earlyInitialize = true ; 96 97 if (earlyInitialize) 98 createDispatcher(configFile) ; 99 else 100 System.setProperty(propertyModelSet, configFile) ; 103 104 try { 106 server = new Server() ; 107 setLogging(false) ; 109 server.addListener(":"+port) ; 110 111 webAppContextJoseki = 112 server.addWebApplication(serverBaseURI, "./webapps/joseki/") ; 113 114 if ( webAppContextJoseki == null ) 115 throw new JosekiServerException("Failed to create the web application (null returned)") ; 116 log.info("Created Joseki server: port="+port+" URI="+serverBaseURI) ; 117 } catch (Exception ex) 118 { 119 log.warn("RDFServer: Failed to create web application server: "+ex) ; 120 } 121 } 122 123 124 132 private void createDispatcher(String configFile) 133 { 134 if (System.getProperty(propertyModelSet) == null) 135 System.setProperty(propertyModelSet, noMapValue); 141 142 try 144 { 145 Dispatcher dispatcher = new Dispatcher(); 148 DispatcherRegistry.getInstance().add(DispatcherName, dispatcher); 149 150 if (configFile != null) 151 dispatcher.getModelSet().load(configFile); 152 else 153 log.info("No initial configuration"); 154 } 155 catch (Exception ex) 156 { 157 if (ex instanceof ConfigurationErrorException) 158 throw (ConfigurationErrorException)ex; 159 throw new ConfigurationErrorException(ex); 160 } 161 } 162 163 164 165 public void start() 166 { 167 try { 168 if ( ! server.isStarted() ) 169 server.start() ; 170 172 } catch (MultiException ex) 173 { 174 List exs = ex.getExceptions() ; 175 java.net.BindException bindException = null ; 176 for ( Iterator iter = exs.iterator() ; iter.hasNext() ; ) 177 { 178 Exception ex2 = (Exception )iter.next() ; 179 if ( ex2 instanceof java.net.BindException ) 180 { 181 bindException = (java.net.BindException )ex2 ; 182 continue ; 183 } 184 log.warn("Exception: "+ex2) ; 185 } 186 187 if ( bindException != null ) 188 { 189 log.error("Bind exception: "+bindException.getMessage()) ; 190 System.exit(99) ; 192 } 193 throw new JosekiServerException("Failed to start web application server") ; 194 } 195 196 if ( ! earlyInitialize ) 197 return ; 198 199 203 try { 204 ClassLoader cl = webAppContextJoseki.getClassLoader() ; 205 206 if ( cl == null ) 207 { 208 log.warn("No classloader for webapp!") ; 209 return ; 210 } 211 212 Class cls = cl.loadClass(DispatcherRegistry.class.getName()); 213 214 if ( ! cls.isAssignableFrom(DispatcherRegistry.class)) 215 { 216 log.warn("Found another dispatcher configuration subsystem in the web apllication"); 217 log.warn("Suspect a second copy of joseki.jar in WEB-INF/lib") ; 218 throw new ConfigurationErrorException("DispatcherRegistry clash") ; 219 } 220 } catch (ClassNotFoundException ex) 221 { 222 log.info("Class not found"); 223 } 224 } 225 226 229 230 public void stop() 231 { 232 try 233 { 234 server.stop() ; 235 } catch (InterruptedException e) 236 { 237 log.warn("Problems stopping server: ",e) ; 238 } 239 240 } 241 242 243 public int getPort() { return port ; } 244 245 public Server getServer() { return server ; } 246 247 250 251 252 private void load(String filename) throws ConfigurationErrorException 253 { 254 Dispatcher disp = DispatcherRegistry.getInstance().find(DispatcherName) ; 256 try { disp.getModelSet().load(filename) ; 257 } catch ( java.io.FileNotFoundException fEx) 258 { 259 throw new ConfigurationErrorException("File not found: "+filename) ; 260 } 261 System.setProperty(propertyModelSet, filename) ; 262 } 265 266 private void setLogging(boolean setOn) 267 { 268 LogSink logSink = null ; 269 270 if (setOn) 271 try 272 { 273 if ( LogFactory.getLog(this.getClass()) instanceof Jdk14Logger ) 275 { 276 LoggerLogSink j14sink = new LoggerLogSink(); 277 j14sink.setLogger(java.util.logging.Logger.getLogger("org.mortbay")); 278 logSink = j14sink ; 279 } 280 281 286 291 logSink.setOptions("tT"); 292 logSink.start(); 293 294 org.mortbay.util.Log.instance().add(logSink); 295 } 296 catch (Exception ioEx) 297 { 298 System.err.println("Exception starting Jetty: " + ioEx); 299 } 300 else 301 { 302 logSink = new NullLogSink() ; 303 } 304 305 306 org.mortbay.util.Log.instance().add(logSink) ; 307 try { logSink.start(); } catch (Exception ex) {} 308 } 310 } 311 312 338 | Popular Tags |