KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > RDFServer


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

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.logging.LoggingControl;
18
import org.joseki.server.http.*;
19 import org.apache.commons.logging.* ;
20
21 import org.apache.commons.logging.impl.Jdk14Logger;
22 //import org.apache.commons.logging.impl.Log4JLogger;
23

24 /** Standalone server.
25  *
26  * @version $Id: RDFServer.java,v 1.15 2004/04/30 14:13:13 andy_seaborne Exp $
27  * @author Andy Seaborne
28  */

29
30
31 public class RDFServer
32 {
33     static final Log log = LogFactory.getLog(RDFServer.class.getName()) ;
34
35     public static final String JavaDoc 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     /** System property for the port number. */
45     public static final String JavaDoc propertyPort = "jena.rdfserver.port" ;
46
47     /** Override the web.xml init-param for the configuration file.
48      */

49     public static final String JavaDoc propertyModelSet = "jena.rdfserver.modelset" ;
50     
51     /** Default port for the server */
52     public static final int defaultPort = 2020 ;
53     
54     /** Default location for the Joseki server */
55     public static final String JavaDoc defaultServerBaseURI = "/" ;
56     
57     /** Default configuration file */
58     public static final String JavaDoc defaultConfigFile = "etc/joseki.n3" ;
59
60     /** Value fo rthe config file meaning "no configuration"
61      */

62     public static final String JavaDoc noMapValue = "<<null>>" ;
63     
64     /** Create a new RDFServer on the default port or as specifed by the system property jena.rdfserver.port */
65     public RDFServer() { this(defaultConfigFile) ; }
66
67     /** Create a new RDFServer using the named configuration file
68      * @param configFile
69      */

70     public RDFServer(String JavaDoc configFile)
71     {
72         String JavaDoc tmp = System.getProperty(propertyPort, defaultPort+"") ;
73         int p = Integer.parseInt(tmp) ;
74         init(configFile, defaultPort, defaultServerBaseURI) ;
75     }
76
77     /** Create a new RDFServer
78      * @param configFile
79      * @param port
80      */

81     public RDFServer(String JavaDoc configFile, int port) { init(configFile, port, defaultServerBaseURI) ; }
82
83     /** Creates new RDFServer using the named configuration file
84      * @param configFile
85      * @param port
86      * @param serverBaseURI
87      */

88     public RDFServer(String JavaDoc configFile, int port, String JavaDoc serverBaseURI)
89     {
90         init(configFile, port, serverBaseURI) ;
91     }
92
93     private void init(String JavaDoc configFile, int port, String JavaDoc serverBaseURI)
94     {
95         boolean earlyInitialize = true ;
96         
97         if (earlyInitialize)
98             createDispatcher(configFile) ;
99         else
100             // Set it so the servlet finds it later
101
// probably during servlet creation on first request.
102
System.setProperty(propertyModelSet, configFile) ;
103
104         // Build the web application and server
105
try {
106             server = new Server() ;
107             // Fix up logging of the Jetty server
108
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 JavaDoc ex)
118         {
119             log.warn("RDFServer: Failed to create web application server: "+ex) ;
120         }
121     }
122
123     
124     // This code creates the dispatcher and the configuration
125
// very early, before the web application exists and before the servlet exists
126
// let alone intitialised.
127
// This means that files read now must be found without the servlet context
128
// (just the current directory and the classpath).
129
// However, it does mean that it happens now and not during the first request
130
// received, which greatly helps simple use (and Joseki development).
131

132     private void createDispatcher(String JavaDoc configFile)
133     {
134         if (System.getProperty(propertyModelSet) == null)
135             // This tells the servlet to load nothing later - not even from the
136
// default location
137
// Rather than know the servlet class, we set a system property.
138
// (Not necessary now as the servlet will find the dispatcher via
139
// the registry)
140
System.setProperty(propertyModelSet, noMapValue);
141
142         // Build the dispatcher
143
try
144         {
145             // Create a dispatcher and register it
146
// The servlet finds it from the registry.
147
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 JavaDoc ex)
156         {
157             if (ex instanceof ConfigurationErrorException)
158                 throw (ConfigurationErrorException)ex;
159             throw new ConfigurationErrorException(ex);
160         }
161     }
162     
163     /** Start the server */
164     
165     public void start()
166     {
167         try {
168             if ( ! server.isStarted() )
169                 server.start() ;
170             //org.mortbay.util.Log.instance().disableLog();
171

172         } catch (MultiException ex)
173         {
174             List exs = ex.getExceptions() ;
175             java.net.BindException JavaDoc bindException = null ;
176             for ( Iterator iter = exs.iterator() ; iter.hasNext() ; )
177             {
178                 Exception JavaDoc ex2 = (Exception JavaDoc)iter.next() ;
179                 if ( ex2 instanceof java.net.BindException JavaDoc )
180                 {
181                     bindException = (java.net.BindException JavaDoc)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.err.println("Bind exception: "+bindException.getMessage()) ;
191
System.exit(99) ;
192             }
193             throw new JosekiServerException("Failed to start web application server") ;
194         }
195         
196         if ( ! earlyInitialize )
197             return ;
198         
199         // Check that the dispatcher registry seen by the webapp is the
200
// same as the one created during initialization. That is, the
201
// webapp does not have its own class for this.
202

203         try {
204             ClassLoader JavaDoc cl = webAppContextJoseki.getClassLoader() ;
205             
206             if ( cl == null )
207             {
208                 log.warn("No classloader for webapp!") ;
209                 return ;
210             }
211
212             Class JavaDoc 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 JavaDoc ex)
221         {
222             log.info("Class not found");
223         }
224     }
225
226     /** Stop the server. On exit from this method, it is <strong>not</strong>
227      * guaranteed that all server threads have ended.
228      */

229     
230     public void stop()
231     {
232         try
233         {
234             server.stop() ;
235         } catch (InterruptedException JavaDoc 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     /** Load a configuration file. See the example file for details
248      * as to the format of this file.
249      */

250     
251     
252     private void load(String JavaDoc filename) throws ConfigurationErrorException
253     {
254         // Now passed into a constructor
255
Dispatcher disp = DispatcherRegistry.getInstance().find(DispatcherName) ;
256         try { disp.getModelSet().load(filename) ;
257         } catch ( java.io.FileNotFoundException JavaDoc fEx)
258         {
259             throw new ConfigurationErrorException("File not found: "+filename) ;
260         }
261         System.setProperty(propertyModelSet, filename) ;
262 // if ( httpServlet != null )
263
// httpServlet.load(filename) ;
264
}
265     
266     private void setLogging(boolean setOn)
267     {
268         LogSink logSink = null ;
269         
270         if (setOn)
271             try
272             {
273                 // Guess the logging system
274
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 // if ( LogFactory.getLog(this.getClass()) instanceof Log4JLogger )
282
// {
283
// logSink = new Log4jSink() ;
284
// }
285

286                 // Create a log sink and initialize it.
287
//System.setProperty("LOG_DATE_FORMAT", "yyyy-mm-dd HH:mm:ss ");
288
//OutputStreamLogSink logSink = new OutputStreamLogSink();
289
//logSink.setOutputStream(System.out);
290

291                 logSink.setOptions("tT");
292                 logSink.start();
293
294                 org.mortbay.util.Log.instance().add(logSink);
295             }
296             catch (Exception JavaDoc 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 JavaDoc ex) {}
308         //disableLog();
309
}
310 }
311
312 /*
313  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
314  * All rights reserved.
315  *
316  * Redistribution and use in source and binary forms, with or without
317  * modification, are permitted provided that the following conditions
318  * are met:
319  * 1. Redistributions of source code must retain the above copyright
320  * notice, this list of conditions and the following disclaimer.
321  * 2. Redistributions in binary form must reproduce the above copyright
322  * notice, this list of conditions and the following disclaimer in the
323  * documentation and/or other materials provided with the distribution.
324  * 3. The name of the author may not be used to endorse or promote products
325  * derived from this software without specific prior written permission.
326  *
327  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
328  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
329  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
330  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
331  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
332  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
333  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
334  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
335  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
336  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
337  */

338
Popular Tags