1 17 18 package org.apache.avalon.cornerstone.blocks.masterstore; 19 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.HashMap ; 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 43 public class RepositoryManager 44 extends AbstractLogEnabled 45 implements Store, Contextualizable, Serviceable, Configurable 46 { 47 private static final String REPOSITORY_NAME = "Repository"; 48 private static long id = 0; 49 50 protected HashMap m_repositories = new HashMap (); 51 protected HashMap m_models = new HashMap (); 52 protected HashMap m_classes = new HashMap (); 53 protected ServiceManager m_serviceManager; 54 protected Context m_context; 55 56 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 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 protocol = protocols[ i ].getValue(); 99 100 for( int j = 0; j < types.length; j++ ) 101 { 102 final String type = types[ j ].getValue(); 103 104 for( int k = 0; k < modelIterator.length; k++ ) 105 { 106 final String 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 service ) 115 { 116 } 117 118 public boolean isSelectable( final Object policy ) 119 { 120 return ( policy instanceof Configuration ); 121 } 122 123 public Object select( final Object policy ) 124 throws ServiceException 125 { 126 Configuration repConf = null; 127 try 128 { 129 repConf = (Configuration)policy; 130 } 131 catch( final ClassCastException cce ) 132 { 133 throw new ServiceException( policy.toString(), "Hint is of the wrong type. " + 134 "Must be a Configuration", cce ); 135 } 136 137 URL destination = null; 138 try 139 { 140 destination = new URL ( 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 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 type = repConf.getAttribute( "type" ); 156 final String repID = destination + type; 157 Repository reply = (Repository)m_repositories.get( repID ); 158 final String model = (String )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 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 protocol = destination.getProtocol(); 176 final String repClass = (String )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 e ) 199 { 200 final String 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 getName() 214 { 215 return REPOSITORY_NAME + id++; 216 } 217 } 218 | Popular Tags |