KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailrepository > filepair > RepositoryManager


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.mailrepository.filepair;
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import org.apache.avalon.cornerstone.services.store.Repository;
24 import org.apache.avalon.cornerstone.services.store.Store;
25 import org.apache.avalon.framework.activity.Initializable;
26 import org.apache.avalon.framework.component.Component;
27 import org.apache.avalon.framework.component.ComponentException;
28 import org.apache.avalon.framework.component.ComponentManager;
29 import org.apache.avalon.framework.component.Composable;
30 import org.apache.avalon.framework.configuration.Configurable;
31 import org.apache.avalon.framework.configuration.Configuration;
32 import org.apache.avalon.framework.configuration.ConfigurationException;
33 import org.apache.avalon.framework.context.Context;
34 import org.apache.avalon.framework.context.Contextualizable;
35 import org.apache.avalon.framework.logger.AbstractLogEnabled;
36 import org.apache.avalon.framework.service.ServiceException;
37 import org.apache.avalon.framework.service.ServiceManager;
38 import org.apache.avalon.framework.service.Serviceable;
39 import org.apache.avalon.framework.container.ContainerUtil;
40
41 /**
42  * @phoenix:block
43  * @phoenix:service name="org.apache.avalon.cornerstone.services.store.Store"
44  *
45  */

46 public class RepositoryManager
47     extends AbstractLogEnabled
48     implements Store, Contextualizable, Composable, Serviceable, Configurable
49 {
50     private static final String JavaDoc REPOSITORY_NAME = "Repository";
51     private static long id = 0;
52
53     protected HashMap JavaDoc m_repositories = new HashMap JavaDoc();
54     protected HashMap JavaDoc m_models = new HashMap JavaDoc();
55     protected HashMap JavaDoc m_classes = new HashMap JavaDoc();
56     protected ComponentManager m_componentManager;
57     protected ServiceManager m_serviceManager;
58     protected Context m_context;
59
60     public void contextualize( final Context context )
61     {
62         m_context = context;
63     }
64
65     public void compose( final ComponentManager componentManager )
66         throws ComponentException
67     {
68         m_componentManager = componentManager;
69     }
70
71     public void service( final ServiceManager serviceManager )
72         throws ServiceException
73     {
74         m_serviceManager = serviceManager;
75     }
76
77     public void configure( final Configuration configuration )
78         throws ConfigurationException
79     {
80         final Configuration[] registeredClasses =
81             configuration.getChild( "repositories" ).getChildren( "repository" );
82
83         for( int i = 0; i < registeredClasses.length; i++ )
84         {
85             registerRepository( registeredClasses[ i ] );
86         }
87     }
88
89     public void registerRepository( final Configuration repConf )
90         throws ConfigurationException
91     {
92         final String JavaDoc className = repConf.getAttribute( "class" );
93         getLogger().info( "Registering Repository " + className );
94
95         final Configuration[] protocols =
96             repConf.getChild( "protocols" ).getChildren( "protocol" );
97         final Configuration[] types = repConf.getChild( "types" ).getChildren( "type" );
98         final Configuration[] modelIterator =
99             repConf.getChild( "models" ).getChildren( "model" );
100
101         for( int i = 0; i < protocols.length; i++ )
102         {
103             final String JavaDoc protocol = protocols[ i ].getValue();
104
105             for( int j = 0; j < types.length; j++ )
106             {
107                 final String JavaDoc type = types[ j ].getValue();
108
109                 for( int k = 0; k < modelIterator.length; k++ )
110                 {
111                     final String JavaDoc model = modelIterator[ k ].getValue();
112                     m_classes.put( protocol + type + model, className );
113                     getLogger().info( " for " + protocol + "," + type + "," + model );
114                 }
115             }
116         }
117     }
118
119     public void release( final Component component )
120     {
121     }
122
123     public boolean hasComponent( final Object JavaDoc hint )
124     {
125         if( hint instanceof Configuration )
126             return true;
127         else
128             return false;
129     }
130
131     public boolean isSelectable( final Object JavaDoc hint )
132     {
133         if( hint instanceof Configuration )
134             return true;
135         else
136             return false;
137     }
138
139     public Component select( final Object JavaDoc hint )
140         throws ComponentException
141     {
142         Configuration repConf = null;
143         try
144         {
145             repConf = (Configuration)hint;
146         }
147         catch( final ClassCastException JavaDoc cce )
148         {
149             throw new ComponentException( "Hint is of the wrong type. " +
150                                           "Must be a Configuration", cce );
151         }
152
153         URL JavaDoc destination = null;
154         try
155         {
156             destination = new URL JavaDoc( repConf.getAttribute( "destinationURL" ) );
157         }
158         catch( final ConfigurationException ce )
159         {
160             throw new ComponentException( "Malformed configuration has no " +
161                                           "destinationURL attribute", ce );
162         }
163         catch( final MalformedURLException JavaDoc mue )
164         {
165             throw new ComponentException( "destination is malformed. " +
166                                           "Must be a valid URL", mue );
167         }
168
169         try
170         {
171             final String JavaDoc type = repConf.getAttribute( "type" );
172             final String JavaDoc repID = destination + type;
173             Repository reply = (Repository)m_repositories.get( repID );
174             final String JavaDoc model = (String JavaDoc)repConf.getAttribute( "model" );
175
176             if( null != reply )
177             {
178                 if( m_models.get( repID ).equals( model ) )
179                 {
180                     return reply;
181                 }
182                 else
183                 {
184                     final String JavaDoc message = "There is already another repository with the " +
185                         "same destination and type but with different model";
186                     throw new ComponentException( message );
187                 }
188             }
189             else
190             {
191                 final String JavaDoc protocol = destination.getProtocol();
192                 final String JavaDoc repClass = (String JavaDoc)m_classes.get( protocol + type + model );
193
194                 getLogger().debug( "Need instance of " + repClass + " to handle: " +
195                                    protocol + type + model );
196
197                 try
198                 {
199                     reply = (Repository)Class.forName( repClass ).newInstance();
200                     setupLogger( reply, "repository" );
201
202                     ContainerUtil.contextualize(reply, m_context);
203                     ContainerUtil.compose(reply, m_componentManager);
204                     ContainerUtil.service(reply, m_serviceManager);
205                     ContainerUtil.configure(reply, repConf);
206                     ContainerUtil.initialize(reply);
207
208                     m_repositories.put( repID, reply );
209                     m_models.put( repID, model );
210                     getLogger().info( "New instance of " + repClass + " created for " +
211                                       destination );
212                     return reply;
213                 }
214                 catch( final Exception JavaDoc e )
215                 {
216                     final String JavaDoc message = "Cannot find or init repository: " + e.getMessage();
217                     getLogger().warn( message, e );
218
219                     throw new ComponentException( message, e );
220                 }
221             }
222         }
223         catch( final ConfigurationException ce )
224         {
225             throw new ComponentException( "Malformed configuration", ce );
226         }
227     }
228
229     public static final String JavaDoc getName()
230     {
231         return REPOSITORY_NAME;
232     }
233 }
234
Popular Tags