1 16 package nl.hippo.cms.repositorylocation; 17 18 import java.io.InputStream ; 19 import java.util.Properties ; 20 import nl.hippo.cms.sitesdirectory.SitesDirectoryImpl; 21 import org.apache.avalon.framework.activity.Initializable; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.framework.parameters.ParameterException; 24 import org.apache.avalon.framework.parameters.Parameterizable; 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.Serviceable; 29 import org.apache.excalibur.source.Source; 30 import org.apache.excalibur.source.SourceResolver; 31 32 56 public class RepositoryLocationImpl extends AbstractLogEnabled 57 implements RepositoryLocation, Serviceable, Parameterizable, Initializable 58 { 59 private static final String CONFIGURATION_URI_PARAMETER_NAME = "configurationUri"; 60 61 private static final String PROPERTY_PREFIX = "repository."; 62 63 private static final String HOST_PROPERTY_SUFFIX = "host"; 64 65 private static final String PORT_PROPERTY_SUFFIX = "port"; 66 67 private static final String REALM_PROPERTY_SUFFIX = "realm"; 68 69 private static final String USERNAME_PROPERTY_SUFFIX = "username"; 70 71 private static final String PASSWORD_PROPERTY_SUFFIX = "password"; 72 73 private static final String ROOT_URI_PROPERTY_SUFFIX = "rootUri"; 74 75 private static final String BASE_URI_PROPERTY_SUFFIX = "baseUri"; 76 77 82 private ServiceManager m_serviceManager; 83 84 90 private String m_configurationUri; 91 92 98 private RepositoryInformation m_repositoryInformation; 99 100 105 public RepositoryLocationImpl() 106 { 107 super(); 108 } 109 110 119 public void service(ServiceManager serviceManager) throws ServiceException 120 { 121 m_serviceManager = serviceManager; 122 } 123 124 132 public void parameterize(Parameters params) throws ParameterException 133 { 134 m_configurationUri = params.getParameter(CONFIGURATION_URI_PARAMETER_NAME); 135 } 136 137 142 public void initialize() throws Exception 143 { 144 Properties properties = new Properties (); 145 Object sourceResolverAsObject = m_serviceManager.lookup(SourceResolver.ROLE); 146 try 147 { 148 if (sourceResolverAsObject instanceof SourceResolver) 149 { 150 SourceResolver sourceResolver = (SourceResolver) sourceResolverAsObject; 151 Source source = sourceResolver.resolveURI(m_configurationUri); 152 try 153 { 154 if (source.exists()) 155 { 156 InputStream input = source.getInputStream(); 157 try 158 { 159 properties.load(input); 160 } 161 finally 162 { 163 input.close(); 164 } 165 } 166 else 167 { 168 throw new IllegalArgumentException ("The properties file specified by '" 169 + m_configurationUri + "' must exist."); 170 } 171 } 172 finally 173 { 174 sourceResolver.release(source); 175 } 176 } 177 } 178 finally 179 { 180 m_serviceManager.release(sourceResolverAsObject); 181 } 182 m_repositoryInformation = retrieveRepositoryInformation(properties); 183 } 184 185 190 public RepositoryInformation getRepositoryInformation() 191 { 192 return m_repositoryInformation; 193 } 194 195 211 private RepositoryInformation retrieveRepositoryInformation(Properties properties) 212 { 213 RepositoryInformation result; 214 215 String host = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX 216 + HOST_PROPERTY_SUFFIX); 217 int port = SitesDirectoryImpl.getRequiredIntegerProperty(properties, PROPERTY_PREFIX 218 + PORT_PROPERTY_SUFFIX, 1, 65535); 219 String realm = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX 220 + REALM_PROPERTY_SUFFIX); 221 String username = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX 222 + USERNAME_PROPERTY_SUFFIX); 223 String password = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX 224 + PASSWORD_PROPERTY_SUFFIX); 225 String rootUri = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX 226 + ROOT_URI_PROPERTY_SUFFIX); 227 String baseUri = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX 228 + BASE_URI_PROPERTY_SUFFIX); 229 result = new RepositoryInformation(host, port, realm, username, password, rootUri, baseUri); 230 231 return result; 232 } 233 } 234 | Popular Tags |