View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl;
20  
21  import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  
29  import java.util.Enumeration;
30  import java.util.HashMap;
31  import java.util.Map;
32  import java.util.Properties;
33  
34  import javax.servlet.ServletContextEvent;
35  import javax.servlet.ServletContextListener;
36  
37  
38  /**
39   * CMIS context listener.
40   */
41  public class CmisRepositoryContextListener implements ServletContextListener
42  {
43      public static final String SERVICES_FACTORY = "org.apache.chemistry.opencmis.servicesfactory";
44      private static final Log log = LogFactory.getLog( CmisRepositoryContextListener.class.getName(  ) );
45      private static final String CONFIG_INIT_PARAM = "org.apache.chemistry.opencmis.REPOSITORY_CONFIG_FILE";
46      private static final String CONFIG_FILENAME = "/repository.properties";
47      private static final String PROPERTY_CLASS = "class";
48  
49      public void contextInitialized( ServletContextEvent sce )
50      {
51          // get config file name or use default
52          String configFilename = sce.getServletContext(  ).getInitParameter( CONFIG_INIT_PARAM );
53  
54          if ( configFilename == null )
55          {
56              configFilename = CONFIG_FILENAME;
57          }
58  
59          // create services factory
60          CmisServiceFactory factory = createServiceFactory( configFilename );
61  
62          // set the services factory into the servlet context
63          sce.getServletContext(  ).setAttribute( SERVICES_FACTORY, factory );
64      }
65  
66      public void contextDestroyed( ServletContextEvent sce )
67      {
68          // destroy services factory
69          CmisServiceFactory factory = (CmisServiceFactory) sce.getServletContext(  ).getAttribute( SERVICES_FACTORY );
70  
71          if ( factory != null )
72          {
73              factory.destroy(  );
74          }
75      }
76  
77      /**
78       * Creates a service factory.
79       */
80      private CmisServiceFactory createServiceFactory( String filename )
81      {
82          // load properties
83          InputStream stream = this.getClass(  ).getResourceAsStream( filename );
84  
85          if ( stream == null )
86          {
87              log.warn( "Cannot find configuration!" );
88  
89              return null;
90          }
91  
92          Properties props = new Properties(  );
93  
94          try
95          {
96              props.load( stream );
97          }
98          catch ( IOException e )
99          {
100             log.warn( "Cannot load configuration: " + e, e );
101 
102             return null;
103         }
104         finally
105         {
106             try
107             {
108                 stream.close(  );
109             }
110             catch ( IOException ioe )
111             {
112             }
113         }
114 
115         // get 'class' property
116         String className = props.getProperty( PROPERTY_CLASS );
117 
118         if ( className == null )
119         {
120             log.warn( "Configuration doesn't contain the property 'class'!" );
121 
122             return null;
123         }
124 
125         // create a factory instance
126         Object object = null;
127 
128         try
129         {
130             object = Class.forName( className ).newInstance(  );
131         }
132         catch ( Exception e )
133         {
134             log.warn( "Could not create a services factory instance: " + e, e );
135 
136             return null;
137         }
138 
139         if ( !( object instanceof CmisServiceFactory ) )
140         {
141             log.warn( "The provided class is not an instance of CmisServiceFactory!" );
142         }
143 
144         CmisServiceFactory factory = (CmisServiceFactory) object;
145 
146         // initialize factory instance
147         Map<String, String> parameters = new HashMap<String, String>(  );
148 
149         for ( Enumeration<?> e = props.propertyNames(  ); e.hasMoreElements(  ); )
150         {
151             String key = (String) e.nextElement(  );
152             String value = props.getProperty( key );
153             parameters.put( key, value );
154         }
155 
156         factory.init( parameters );
157 
158         log.info( "Initialized Services Factory: " + factory.getClass(  ).getName(  ) );
159 
160         return factory;
161     }
162 }