KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > repository > RepositoryManager


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

16 package org.apache.cocoon.components.repository;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.component.Component;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.Serviceable;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31 import org.apache.cocoon.ProcessingException;
32 import org.apache.cocoon.components.LifecycleHelper;
33 import org.apache.cocoon.components.repository.helpers.CredentialsToken;
34
35
36 /**
37  * A factory component to create instances of repositories.
38  */

39 public class RepositoryManager extends AbstractLogEnabled
40 implements Serviceable, Disposable, Configurable, Component, ThreadSafe {
41
42     /** The Avalon role name */
43     public static final String JavaDoc ROLE = RepositoryManager.class.getName();
44     
45     /* The ServiceManager */
46     private ServiceManager manager;
47
48     /* A HashMap holding the repositories configurations */
49     private Map JavaDoc repos = new HashMap JavaDoc();
50
51     /* (non-Javadoc)
52      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
53      */

54     public void service(ServiceManager manager) throws ServiceException {
55         this.manager = manager;
56     }
57
58     /* (non-Javadoc)
59      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
60      */

61     public void configure(Configuration configuration) throws ConfigurationException {
62
63         if (this.getLogger().isDebugEnabled()) {
64             this.getLogger().debug("configuring repository manager");
65         }
66
67         Configuration[] children = configuration.getChildren();
68         for (int i = 0; i < children.length; i++) {
69
70             if (this.getLogger().isDebugEnabled()) {
71                 this.getLogger().debug("found repository: " + children[i].getAttribute("class"));
72             }
73             this.repos.put(children[i].getAttribute("name"), children[i]);
74         }
75     }
76
77     /* (non-Javadoc)
78      * @see org.apache.avalon.framework.activity.Disposable#dispose()
79      */

80     public void dispose() {
81         this.manager = null;
82     }
83
84     /**
85      * get instance of repository.
86      *
87      * @param hint identifies the repository implementation to load.
88      * @param credentials the user credentials the repository instance is initialized with.
89      * @return the repository instance.
90      */

91     public Repository getRepository(String JavaDoc hint, CredentialsToken credentials) throws ProcessingException {
92
93         if (this.getLogger().isDebugEnabled()) {
94             this.getLogger().debug("get repository for: " + hint);
95         }
96         
97         String JavaDoc className = null;
98
99         try {
100     
101             Configuration repoConfiguration = (Configuration)this.repos.get(hint);
102             className = repoConfiguration.getAttribute("class");
103             Class JavaDoc repoClass = Class.forName(className);
104     
105             if (this.getLogger().isDebugEnabled()) {
106                 this.getLogger().debug("loading class" + className);
107             }
108     
109             Repository repo = (Repository) repoClass.newInstance();
110             LifecycleHelper.setupComponent(repo,
111                                            this.getLogger(),
112                                            null,
113                                            this.manager,
114                                            repoConfiguration,
115                                            true);
116             repo.setCredentials(credentials);
117             return repo;
118
119         } catch (ConfigurationException ce) {
120             throw new ProcessingException("Could not get configuration for " + hint, ce);
121         } catch (ClassNotFoundException JavaDoc cnfe) {
122             throw new ProcessingException("Could not load class " + className, cnfe);
123         } catch (InstantiationException JavaDoc ie) {
124             throw new ProcessingException("Could not instantiate class " + className, ie);
125         } catch (IllegalAccessException JavaDoc iae) {
126              throw new ProcessingException("Could not instantiate class " + className, iae);
127         } catch (Exception JavaDoc e) {
128              throw new ProcessingException("Could not setup component " + className, e);
129         }
130     }
131     
132 }
Popular Tags