1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
52 String configFilename = sce.getServletContext( ).getInitParameter( CONFIG_INIT_PARAM );
53
54 if ( configFilename == null )
55 {
56 configFilename = CONFIG_FILENAME;
57 }
58
59
60 CmisServiceFactory factory = createServiceFactory( configFilename );
61
62
63 sce.getServletContext( ).setAttribute( SERVICES_FACTORY, factory );
64 }
65
66 public void contextDestroyed( ServletContextEvent sce )
67 {
68
69 CmisServiceFactory factory = (CmisServiceFactory) sce.getServletContext( ).getAttribute( SERVICES_FACTORY );
70
71 if ( factory != null )
72 {
73 factory.destroy( );
74 }
75 }
76
77
78
79
80 private CmisServiceFactory createServiceFactory( String filename )
81 {
82
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
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
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
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 }