Author: angelo.vandersijpt at luminis.eu
Date: Mon Oct 18 15:02:44 2010
New Revision: 184
Log:
AMDATU-103 Refactored ConfigTemplateManager to use config admin in a nicer way,
and introduced the File API here.
Modified:
trunk/platform-bundles/config-template-manager/src/main/java/org/amdatu/platform/configtemplatemanager/osgi/Activator.java
trunk/platform-bundles/config-template-manager/src/main/java/org/amdatu/platform/configtemplatemanager/service/ConfigTemplateManagerImpl.java
Modified:
trunk/platform-bundles/config-template-manager/src/main/java/org/amdatu/platform/configtemplatemanager/osgi/Activator.java
==============================================================================
---
trunk/platform-bundles/config-template-manager/src/main/java/org/amdatu/platform/configtemplatemanager/osgi/Activator.java
(original)
+++
trunk/platform-bundles/config-template-manager/src/main/java/org/amdatu/platform/configtemplatemanager/osgi/Activator.java
Mon Oct 18 15:02:44 2010
@@ -47,7 +47,8 @@
.setImplementation(ConfigTemplateManagerImpl.class)
.add(createServiceDependency().setService(LogService.class).setRequired(true))
.add(createServiceDependency().setService(HttpContextServiceFactory.class).setRequired(true))
-
.add(createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true)));
+
.add(createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true))
+
.add(createConfigurationDependency().setPid(ConfigTemplateManagerImpl.PID)));
}
/**
Modified:
trunk/platform-bundles/config-template-manager/src/main/java/org/amdatu/platform/configtemplatemanager/service/ConfigTemplateManagerImpl.java
==============================================================================
---
trunk/platform-bundles/config-template-manager/src/main/java/org/amdatu/platform/configtemplatemanager/service/ConfigTemplateManagerImpl.java
(original)
+++
trunk/platform-bundles/config-template-manager/src/main/java/org/amdatu/platform/configtemplatemanager/service/ConfigTemplateManagerImpl.java
Mon Oct 18 15:02:44 2010
@@ -27,6 +27,7 @@
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
+import java.util.Dictionary;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
@@ -40,18 +41,20 @@
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
import org.osgi.service.log.LogService;
/**
* This class implements the Config Template Manager.
* @author ivol
*/
-public class ConfigTemplateManagerImpl implements ConfigTemplateManager,
ResourceProvider {
- // Regular expression to match configruation entries. Syntax:
${[pid].[configentry]}
+public class ConfigTemplateManagerImpl implements ConfigTemplateManager,
ResourceProvider, ManagedService {
+ // Regular expression to match configuration entries. Syntax:
${[pid].[configentry]}
private static final Pattern CONFIG_ENTRY_REGEX =
Pattern.compile("\\$\\{([A-Za-z0-9\\.-])+/([A-Za-z0-9\\.-])+\\}");
// The PID of the configuration of this bundle
- private final static String PID =
ConfigTemplateManager.class.getPackage().getName();
+ public final static String PID =
ConfigTemplateManager.class.getPackage().getName();
// Services injected by the Felix dependency manager
private volatile LogService m_logService;
@@ -59,18 +62,22 @@
private volatile HttpContextServiceFactory m_httpContextFactoryService;
// Instances injected by the Felix dependency manager
- private BundleContext m_bundleContext;
+ private volatile BundleContext m_bundleContext;
- // The work directory to use
- private String m_workDir;
+ // Configured properties
+ private String m_workDirName;
private String m_hostName;
- private String m_portNr;
+ private int m_portNr;
+
+ // Our working directory
+ private File m_workDir;
// Private int that stores the latest id assigned to a requested resource
private int m_latestId = 0;
// Hashmap that caches converted URLs. The cache maps the original URL as
String onto
// the URL of the converted file
+ // TODO Why is this 'string-string' in stead of 'string-url'?
private Map<String, String> m_urlCache = new ConcurrentHashMap<String,
String>();
// Our private HTTP context service
@@ -83,37 +90,23 @@
// Create our own http context and register resources
m_httpContextComponent =
m_httpContextFactoryService.create(m_bundleContext, this);
- Configuration config = null;
- try {
- // Wait until our config is made available by felix install bundle
- while (config == null || config.getProperties() == null) {
- config = m_configurationAdmin.getConfiguration(PID);
- if (config == null || config.getProperties() == null) {
- try {
- Thread.sleep(500);
- m_logService.log(LogService.LOG_INFO, PID + "
configuration not yet available, waiting for 500 milliseconds");
- } catch (InterruptedException e) {
- }
- }
- }
- String workBaseDir = System.getProperty("user.dir") +
File.separator + "work";
- m_workDir = workBaseDir + File.separator +
config.getProperties().get("workdir").toString();
- m_hostName = config.getProperties().get("hostname").toString();
- m_portNr = config.getProperties().get("port").toString();
- new File(m_workDir).mkdirs();
- } catch (IOException e) {
- m_logService.log(LogService.LOG_ERROR, "Could not create work
directory '" + m_workDir + "'", e);
- }
-
+ initializeWorkDir();
+
m_logService.log(LogService.LOG_INFO, getClass().getName() + " service
initialized");
}
-
+
+ private void initializeWorkDir() {
+ File workBaseDir = new File(System.getProperty("user.dir"), "work");
+ m_workDir = new File(workBaseDir, m_workDirName);
+ m_workDir.mkdirs();
+ }
+
/**
* destroy() method invoked by the Felix dependency manager
*/
public void destroy() {
m_httpContextComponent.stop();
-
+ // TODO do we need to cleanup our work dir? Or does it stick around
until the next session?
m_logService.log(LogService.LOG_INFO, getClass().getName() + " service
destroyed");
}
@@ -137,17 +130,17 @@
int urlId = getURLId();
// Determine the filename for the converted file
- String filename = "/" + urlId + ".config";
+ String filename = urlId + ".config";
// Create the temporary file to write the converted configuration
template to
- configFile = new File(m_workDir + filename);
+ configFile = new File(m_workDir, filename);
// Write the file with replaced configuration entries to the target
file
writeConfiguration(templateURL, configFile);
// Return the new file URL
- URL url = new URL("http://" + m_hostName + ":" + m_portNr + "/" +
Activator.RESOURCE_ID + filename);
- m_urlCache.put(templateURL.toString(), url.toString());
+ URL url = new URL("http", m_hostName, m_portNr, "/" +
Activator.RESOURCE_ID + "/" + filename);
+ m_urlCache.put(templateURL.toString(), url.toExternalForm());
return url;
}
@@ -233,7 +226,7 @@
String url = m_urlCache.get(templateURL);
m_urlCache.remove(templateURL);
String configId = url.substring(url.lastIndexOf("/") + 1);
- File file = new File(m_workDir + "/" + configId);
+ File file = new File(m_workDir, configId);
if (file.exists()) {
if (!file.delete()) {
m_logService.log(LogService.LOG_INFO, "Failed to remove
file '" + file.getAbsolutePath()
@@ -292,11 +285,11 @@
*/
public URL getResource(String name) {
String configId = name.substring(name.lastIndexOf("/") + 1);
- String resUrl = ("file:" + m_workDir + File.separator +
configId).replace(File.separator, "/");
+
try {
- return URI.create(resUrl).toURL();
+ return new File(m_workDir, configId).toURI().toURL();
} catch (MalformedURLException e) {
- m_logService.log(LogService.LOG_WARNING, "Calculated resource URL
'" + resUrl + "' is invalid", e);
+ m_logService.log(LogService.LOG_WARNING, "Calculated resource URL
for '" + name + "' is invalid", e);
}
return null;
@@ -309,10 +302,19 @@
return Activator.RESOURCE_ID;
}
- /**
- * {@inheritDoc}
- */
- public long getBundleId() {
- return m_bundleContext.getBundle().getBundleId();
+ public void updated(Dictionary dictionary) throws ConfigurationException {
+ checkAvailability(dictionary, "workdir", "hostname", "port");
+ m_workDirName = dictionary.get("workdir").toString();
+ m_hostName = dictionary.get("hostname").toString();
+ m_portNr = Integer.parseInt(dictionary.get("port").toString());
}
+
+ private void checkAvailability(Dictionary dictionary, String...
mandatoryKeys) throws ConfigurationException {
+ for (String mandatoryKey : mandatoryKeys) {
+ if (dictionary.get(mandatoryKey) == null) {
+ throw new ConfigurationException("Missing configuration key",
mandatoryKey);
+ }
+ }
+ }
+
}