001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.repository;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.activity.Disposable;
023: import org.apache.avalon.framework.component.Component;
024: import org.apache.avalon.framework.configuration.Configurable;
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.logger.AbstractLogEnabled;
028: import org.apache.avalon.framework.service.ServiceException;
029: import org.apache.avalon.framework.service.ServiceManager;
030: import org.apache.avalon.framework.service.Serviceable;
031: import org.apache.avalon.framework.thread.ThreadSafe;
032: import org.apache.cocoon.ProcessingException;
033: import org.apache.cocoon.components.LifecycleHelper;
034: import org.apache.cocoon.components.repository.helpers.CredentialsToken;
035:
036: /**
037: * A factory component to create instances of repositories.
038: */
039: public class RepositoryManager extends AbstractLogEnabled implements
040: Serviceable, Disposable, Configurable, Component, ThreadSafe {
041:
042: /** The Avalon role name */
043: public static final String ROLE = RepositoryManager.class.getName();
044:
045: /* The ServiceManager */
046: private ServiceManager manager;
047:
048: /* A HashMap holding the repositories configurations */
049: private Map repos = new HashMap();
050:
051: /* (non-Javadoc)
052: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
053: */
054: public void service(ServiceManager manager) throws ServiceException {
055: this .manager = manager;
056: }
057:
058: /* (non-Javadoc)
059: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
060: */
061: public void configure(Configuration configuration)
062: throws ConfigurationException {
063:
064: if (this .getLogger().isDebugEnabled()) {
065: this .getLogger().debug("configuring repository manager");
066: }
067:
068: Configuration[] children = configuration.getChildren();
069: for (int i = 0; i < children.length; i++) {
070:
071: if (this .getLogger().isDebugEnabled()) {
072: this .getLogger().debug(
073: "found repository: "
074: + children[i].getAttribute("class"));
075: }
076: this .repos.put(children[i].getAttribute("name"),
077: children[i]);
078: }
079: }
080:
081: /* (non-Javadoc)
082: * @see org.apache.avalon.framework.activity.Disposable#dispose()
083: */
084: public void dispose() {
085: this .manager = null;
086: }
087:
088: /**
089: * get instance of repository.
090: *
091: * @param hint identifies the repository implementation to load.
092: * @param credentials the user credentials the repository instance is initialized with.
093: * @return the repository instance.
094: */
095: public Repository getRepository(String hint,
096: CredentialsToken credentials) throws ProcessingException {
097:
098: if (this .getLogger().isDebugEnabled()) {
099: this .getLogger().debug("get repository for: " + hint);
100: }
101:
102: String className = null;
103:
104: try {
105:
106: Configuration repoConfiguration = (Configuration) this .repos
107: .get(hint);
108: className = repoConfiguration.getAttribute("class");
109: Class repoClass = Class.forName(className);
110:
111: if (this .getLogger().isDebugEnabled()) {
112: this .getLogger().debug("loading class" + className);
113: }
114:
115: Repository repo = (Repository) repoClass.newInstance();
116: LifecycleHelper.setupComponent(repo, this .getLogger(),
117: null, this .manager, repoConfiguration, true);
118: repo.setCredentials(credentials);
119: return repo;
120:
121: } catch (ConfigurationException ce) {
122: throw new ProcessingException(
123: "Could not get configuration for " + hint, ce);
124: } catch (ClassNotFoundException cnfe) {
125: throw new ProcessingException("Could not load class "
126: + className, cnfe);
127: } catch (InstantiationException ie) {
128: throw new ProcessingException(
129: "Could not instantiate class " + className, ie);
130: } catch (IllegalAccessException iae) {
131: throw new ProcessingException(
132: "Could not instantiate class " + className, iae);
133: } catch (Exception e) {
134: throw new ProcessingException("Could not setup component "
135: + className, e);
136: }
137: }
138:
139: }
|