KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > masterstore > RepositoryManager


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

17
18 package org.apache.avalon.cornerstone.blocks.masterstore;
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import org.apache.avalon.cornerstone.services.store.Repository;
25 import org.apache.avalon.cornerstone.services.store.Store;
26
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.container.ContainerUtil;
31 import org.apache.avalon.framework.context.Context;
32 import org.apache.avalon.framework.context.Contextualizable;
33 import org.apache.avalon.framework.logger.AbstractLogEnabled;
34 import org.apache.avalon.framework.service.ServiceException;
35 import org.apache.avalon.framework.service.ServiceManager;
36 import org.apache.avalon.framework.service.Serviceable;
37
38 /**
39  * @author <a HREF="mailto:fede@apache.org">Federico Barbieri</a>
40  * @avalon.component name="repository-manager" lifestyle="singleton"
41  * @avalon.service type="org.apache.avalon.cornerstone.services.store.Store"
42  */

43 public class RepositoryManager
44     extends AbstractLogEnabled
45     implements Store, Contextualizable, Serviceable, Configurable
46 {
47     private static final String JavaDoc REPOSITORY_NAME = "Repository";
48     private static long id = 0;
49
50     protected HashMap JavaDoc m_repositories = new HashMap JavaDoc();
51     protected HashMap JavaDoc m_models = new HashMap JavaDoc();
52     protected HashMap JavaDoc m_classes = new HashMap JavaDoc();
53     protected ServiceManager m_serviceManager;
54     protected Context m_context;
55
56    /**
57     * @avalon.entry key="urn:avalon:name" alias="block.name"
58     * @avalon.entry key="urn:avalon:partition" alias="app.name"
59     * @avalon.entry key="urn:avalon:home" type="java.io.File" alias="app.home"
60     */

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