1 16 package org.apache.cocoon.components.repository; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 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 39 public class RepositoryManager extends AbstractLogEnabled 40 implements Serviceable, Disposable, Configurable, Component, ThreadSafe { 41 42 43 public static final String ROLE = RepositoryManager.class.getName(); 44 45 46 private ServiceManager manager; 47 48 49 private Map repos = new HashMap (); 50 51 54 public void service(ServiceManager manager) throws ServiceException { 55 this.manager = manager; 56 } 57 58 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 80 public void dispose() { 81 this.manager = null; 82 } 83 84 91 public Repository getRepository(String hint, CredentialsToken credentials) throws ProcessingException { 92 93 if (this.getLogger().isDebugEnabled()) { 94 this.getLogger().debug("get repository for: " + hint); 95 } 96 97 String className = null; 98 99 try { 100 101 Configuration repoConfiguration = (Configuration)this.repos.get(hint); 102 className = repoConfiguration.getAttribute("class"); 103 Class 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 cnfe) { 122 throw new ProcessingException("Could not load class " + className, cnfe); 123 } catch (InstantiationException ie) { 124 throw new ProcessingException("Could not instantiate class " + className, ie); 125 } catch (IllegalAccessException iae) { 126 throw new ProcessingException("Could not instantiate class " + className, iae); 127 } catch (Exception e) { 128 throw new ProcessingException("Could not setup component " + className, e); 129 } 130 } 131 132 } | Popular Tags |