1 17 18 package org.apache.james.mailrepository.filepair; 19 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.HashMap ; 23 import org.apache.avalon.cornerstone.services.store.Repository; 24 import org.apache.avalon.cornerstone.services.store.Store; 25 import org.apache.avalon.framework.activity.Initializable; 26 import org.apache.avalon.framework.component.Component; 27 import org.apache.avalon.framework.component.ComponentException; 28 import org.apache.avalon.framework.component.ComponentManager; 29 import org.apache.avalon.framework.component.Composable; 30 import org.apache.avalon.framework.configuration.Configurable; 31 import org.apache.avalon.framework.configuration.Configuration; 32 import org.apache.avalon.framework.configuration.ConfigurationException; 33 import org.apache.avalon.framework.context.Context; 34 import org.apache.avalon.framework.context.Contextualizable; 35 import org.apache.avalon.framework.logger.AbstractLogEnabled; 36 import org.apache.avalon.framework.service.ServiceException; 37 import org.apache.avalon.framework.service.ServiceManager; 38 import org.apache.avalon.framework.service.Serviceable; 39 import org.apache.avalon.framework.container.ContainerUtil; 40 41 46 public class RepositoryManager 47 extends AbstractLogEnabled 48 implements Store, Contextualizable, Composable, Serviceable, Configurable 49 { 50 private static final String REPOSITORY_NAME = "Repository"; 51 private static long id = 0; 52 53 protected HashMap m_repositories = new HashMap (); 54 protected HashMap m_models = new HashMap (); 55 protected HashMap m_classes = new HashMap (); 56 protected ComponentManager m_componentManager; 57 protected ServiceManager m_serviceManager; 58 protected Context m_context; 59 60 public void contextualize( final Context context ) 61 { 62 m_context = context; 63 } 64 65 public void compose( final ComponentManager componentManager ) 66 throws ComponentException 67 { 68 m_componentManager = componentManager; 69 } 70 71 public void service( final ServiceManager serviceManager ) 72 throws ServiceException 73 { 74 m_serviceManager = serviceManager; 75 } 76 77 public void configure( final Configuration configuration ) 78 throws ConfigurationException 79 { 80 final Configuration[] registeredClasses = 81 configuration.getChild( "repositories" ).getChildren( "repository" ); 82 83 for( int i = 0; i < registeredClasses.length; i++ ) 84 { 85 registerRepository( registeredClasses[ i ] ); 86 } 87 } 88 89 public void registerRepository( final Configuration repConf ) 90 throws ConfigurationException 91 { 92 final String className = repConf.getAttribute( "class" ); 93 getLogger().info( "Registering Repository " + className ); 94 95 final Configuration[] protocols = 96 repConf.getChild( "protocols" ).getChildren( "protocol" ); 97 final Configuration[] types = repConf.getChild( "types" ).getChildren( "type" ); 98 final Configuration[] modelIterator = 99 repConf.getChild( "models" ).getChildren( "model" ); 100 101 for( int i = 0; i < protocols.length; i++ ) 102 { 103 final String protocol = protocols[ i ].getValue(); 104 105 for( int j = 0; j < types.length; j++ ) 106 { 107 final String type = types[ j ].getValue(); 108 109 for( int k = 0; k < modelIterator.length; k++ ) 110 { 111 final String model = modelIterator[ k ].getValue(); 112 m_classes.put( protocol + type + model, className ); 113 getLogger().info( " for " + protocol + "," + type + "," + model ); 114 } 115 } 116 } 117 } 118 119 public void release( final Component component ) 120 { 121 } 122 123 public boolean hasComponent( final Object hint ) 124 { 125 if( hint instanceof Configuration ) 126 return true; 127 else 128 return false; 129 } 130 131 public boolean isSelectable( final Object hint ) 132 { 133 if( hint instanceof Configuration ) 134 return true; 135 else 136 return false; 137 } 138 139 public Component select( final Object hint ) 140 throws ComponentException 141 { 142 Configuration repConf = null; 143 try 144 { 145 repConf = (Configuration)hint; 146 } 147 catch( final ClassCastException cce ) 148 { 149 throw new ComponentException( "Hint is of the wrong type. " + 150 "Must be a Configuration", cce ); 151 } 152 153 URL destination = null; 154 try 155 { 156 destination = new URL ( repConf.getAttribute( "destinationURL" ) ); 157 } 158 catch( final ConfigurationException ce ) 159 { 160 throw new ComponentException( "Malformed configuration has no " + 161 "destinationURL attribute", ce ); 162 } 163 catch( final MalformedURLException mue ) 164 { 165 throw new ComponentException( "destination is malformed. " + 166 "Must be a valid URL", mue ); 167 } 168 169 try 170 { 171 final String type = repConf.getAttribute( "type" ); 172 final String repID = destination + type; 173 Repository reply = (Repository)m_repositories.get( repID ); 174 final String model = (String )repConf.getAttribute( "model" ); 175 176 if( null != reply ) 177 { 178 if( m_models.get( repID ).equals( model ) ) 179 { 180 return reply; 181 } 182 else 183 { 184 final String message = "There is already another repository with the " + 185 "same destination and type but with different model"; 186 throw new ComponentException( message ); 187 } 188 } 189 else 190 { 191 final String protocol = destination.getProtocol(); 192 final String repClass = (String )m_classes.get( protocol + type + model ); 193 194 getLogger().debug( "Need instance of " + repClass + " to handle: " + 195 protocol + type + model ); 196 197 try 198 { 199 reply = (Repository)Class.forName( repClass ).newInstance(); 200 setupLogger( reply, "repository" ); 201 202 ContainerUtil.contextualize(reply, m_context); 203 ContainerUtil.compose(reply, m_componentManager); 204 ContainerUtil.service(reply, m_serviceManager); 205 ContainerUtil.configure(reply, repConf); 206 ContainerUtil.initialize(reply); 207 208 m_repositories.put( repID, reply ); 209 m_models.put( repID, model ); 210 getLogger().info( "New instance of " + repClass + " created for " + 211 destination ); 212 return reply; 213 } 214 catch( final Exception e ) 215 { 216 final String message = "Cannot find or init repository: " + e.getMessage(); 217 getLogger().warn( message, e ); 218 219 throw new ComponentException( message, e ); 220 } 221 } 222 } 223 catch( final ConfigurationException ce ) 224 { 225 throw new ComponentException( "Malformed configuration", ce ); 226 } 227 } 228 229 public static final String getName() 230 { 231 return REPOSITORY_NAME; 232 } 233 } 234 | Popular Tags |