Author: [email protected]
Date: Tue Dec 13 13:01:59 2011
New Revision: 1848
Log:
AMDATU-476 Changed Config Templates required dir configuration to using
framework storage
Removed:
trunk/amdatu-core/config-templates/src/main/resources/OSGI-INF/metatype/metatype.xml
Modified:
trunk/amdatu-core/config-templates/src/main/java/org/amdatu/core/config/templates/osgi/Activator.java
trunk/amdatu-core/config-templates/src/main/java/org/amdatu/core/config/templates/service/ConfigTemplateManagerImpl.java
trunk/amdatu-release/src/main/resources/config/amdatu-core-config.xml
Modified:
trunk/amdatu-core/config-templates/src/main/java/org/amdatu/core/config/templates/osgi/Activator.java
==============================================================================
---
trunk/amdatu-core/config-templates/src/main/java/org/amdatu/core/config/templates/osgi/Activator.java
(original)
+++
trunk/amdatu-core/config-templates/src/main/java/org/amdatu/core/config/templates/osgi/Activator.java
Tue Dec 13 13:01:59 2011
@@ -25,7 +25,8 @@
/**
* This is the activator for the config template manager bundle
- * @author ivol
+ *
+ * @author <a href="mailto:[email protected]">Amdatu Project
Team</a>
*/
public class Activator extends DependencyActivatorBase {
@@ -34,7 +35,6 @@
manager.add(createComponent()
.setInterface(ConfigTemplateManager.class.getName(), null)
.setImplementation(ConfigTemplateManagerImpl.class)
-
.add(createConfigurationDependency().setPid(ConfigTemplateManagerImpl.PID))
.add(createServiceDependency()
.setService(ConfigurationAdmin.class)
.setRequired(true))
Modified:
trunk/amdatu-core/config-templates/src/main/java/org/amdatu/core/config/templates/service/ConfigTemplateManagerImpl.java
==============================================================================
---
trunk/amdatu-core/config-templates/src/main/java/org/amdatu/core/config/templates/service/ConfigTemplateManagerImpl.java
(original)
+++
trunk/amdatu-core/config-templates/src/main/java/org/amdatu/core/config/templates/service/ConfigTemplateManagerImpl.java
Tue Dec 13 13:01:59 2011
@@ -13,276 +13,274 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.amdatu.core.config.templates.service;
-
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.StringWriter;
-import java.net.MalformedURLException;
-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;
-import java.util.regex.Pattern;
-
-import org.amdatu.core.config.templates.ConfigTemplateCallbackHandler;
-import org.amdatu.core.config.templates.ConfigTemplateManager;
-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,
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\\.-_])+\\}");
-
- // Prefix for System property replacement
- private static final String SYSTEM_PROPERTY = "system";
-
- // Services injected by the Felix dependency manager
- private volatile LogService m_logService;
- private volatile ConfigurationAdmin m_configurationAdmin;
-
- // Configured properties
- private String m_workDirName;
-
- // 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>();
-
- /**
- * init() method invoked by the Felix dependency manager
- */
- public void init() {
- initializeWorkDir();
- m_logService.log(LogService.LOG_INFO, getClass().getName() + " service
initialized");
- }
-
- private void initializeWorkDir() {
-
- // If amdatu.dir is provided as System property, use this as base
directory instead of the current directory
- File workBaseDir;
- if (System.getProperty("amdatu.dir") != null) {
- workBaseDir = new File(System.getProperty("amdatu.dir"), "work");
- } else {
- workBaseDir = new File(System.getProperty("user.dir"), "work");
- }
- m_workDir = new File(workBaseDir, m_workDirName);
- m_workDir.mkdirs();
- m_logService.log(LogService.LOG_INFO, "ConfigTemplateManager work
directory=" + m_workDir.getAbsolutePath());
- }
-
- /**
- * destroy() method invoked by the Felix dependency manager
- */
- public void destroy() {
- // 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");
- }
-
- public synchronized URL getConfigurationAsURL(URL templateURL) throws
IOException {
- if (m_urlCache.containsKey(templateURL.toString())) {
- String cachedUrl = m_urlCache.get(templateURL.toString());
- try {
- return new URL(cachedUrl);
- } catch (MalformedURLException e) {
- m_logService.log(LogService.LOG_ERROR, "Could not create URL
from cached URI'" + cachedUrl
- + "'. Refreshing cache", e);
- m_urlCache.remove(templateURL.toString());
- }
- }
-
- // Get the temporary directory for web files
- File configFile;
- int urlId = getURLId();
-
- // Determine the filename for the converted file
- String filename = urlId + ".config";
-
- // Create the temporary file to write the converted configuration
template to
- 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 = configFile.toURI().toURL();
- m_urlCache.put(templateURL.toString(), url.toExternalForm());
- return url;
- }
-
- public synchronized StringBuffer getConfigurationAsString(URL templateURL)
throws IOException {
- // Write the file with replaced configuration entries to the target
file
- BufferedWriter writer = null;
- StringWriter sWiter = new StringWriter();
- try {
- writer = new BufferedWriter(sWiter);
- writeConfiguration(templateURL, writer);
- } finally {
- if (writer != null) {
- writer.close();
- }
- }
- return sWiter.getBuffer();
- }
-
- public void writeConfiguration(URL templateURL, File targetFile) throws
IOException {
- writeConfiguration(templateURL, targetFile, null);
- }
-
- public void writeConfiguration(URL templateURL, File targetFile,
ConfigTemplateCallbackHandler handler) throws IOException {
- if (!targetFile.exists()) {
- if (targetFile.getParentFile() != null &&
!targetFile.getParentFile().exists()) {
- if (!targetFile.getParentFile().mkdir()) {
- throw new IOException("Could not create parent directory '"
- + targetFile.getParentFile().getAbsolutePath() + "'");
- }
- }
- if (!targetFile.createNewFile()) {
- throw new IOException("Could not create file '" +
targetFile.getAbsolutePath() + "'");
- }
- }
-
- // Now write the converted file to disk
- BufferedWriter writer = null;
- try {
- writer = new BufferedWriter(new FileWriter(targetFile));
- writeConfiguration(templateURL, writer, handler);
- } finally {
- if (writer != null) {
- writer.close();
- }
- }
- }
-
- public void writeConfiguration(URL templateURL, BufferedWriter writer)
throws IOException {
- writeConfiguration(templateURL, writer, null);
- }
-
- public void writeConfiguration(URL templateURL, BufferedWriter writer,
ConfigTemplateCallbackHandler handler) throws IOException {
- // Now write the converted entries to the writer
- BufferedReader reader = null;
- try {
- try {
- URLConnection inputConnection = templateURL.openConnection();
- reader = new BufferedReader(new
InputStreamReader(inputConnection.getInputStream()));
- String inputLine;
- String outputLine;
- while ((inputLine = reader.readLine()) != null) {
- outputLine = replaceConfigEntries(inputLine, handler);
- writer.write(outputLine + "\r\n");
- }
- } finally {
- if (reader != null) {
- reader.close();
- }
- }
- } finally {
- if (writer != null) {
- writer.close();
- }
- }
- }
-
- public synchronized void clear(String templateURL) {
- if (m_urlCache.containsKey(templateURL)) {
- String url = m_urlCache.get(templateURL);
- m_urlCache.remove(templateURL);
- String configId = url.substring(url.lastIndexOf("/") + 1);
- 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()
- + "' from cache");
- }
- }
- }
- }
-
- /**
- * Returns a guaranteed unique identifier to be used for the URL mapping
- * @return Identifier to use for a URL mapping
- */
- private synchronized int getURLId() {
- m_latestId++;
- return m_latestId;
- }
-
- /**
- * Replaces configuration entries in the line
- * @param line
- * @return
- */
- private String replaceConfigEntries(String line,
ConfigTemplateCallbackHandler handler) {
- // For performance reasons, return immediately if the line does not
contain ${
- if (line.indexOf("${") == -1) {
- return line;
- }
-
- String newLine = line;
- Matcher matcher = CONFIG_ENTRY_REGEX.matcher(line);
- while (matcher.find()) {
- String key = matcher.group();
- String pid = key.substring("${".length(), key.indexOf("/"));
- String configentry = key.substring(key.indexOf("/") + 1,
key.length() - 1);
- Configuration config;
- try {
- config = m_configurationAdmin.getConfiguration(pid, null);
- if (pid.equalsIgnoreCase(SYSTEM_PROPERTY)) {
- // This is a system property, replace it
- String value = System.getProperty(configentry);
- if (value == null) {
- value = "";
- }
- newLine = newLine.replace(key, value);
- } else if (config != null) {
- Object entry = config.getProperties().get(configentry);
- if (entry != null) {
- String value = entry.toString();
- if (handler != null) {
- value = handler.getValue(pid, configentry, entry);
- newLine = newLine.replace(key, value);
- } else {
- newLine = newLine.replace(key, value);
- }
- }
- }
- } catch (IOException e) {
- m_logService.log(LogService.LOG_WARNING, "Configuration for
pid '" + pid
- + "' does not exist, entry ignored", e);
- }
- }
- return newLine;
- }
-
- @SuppressWarnings("rawtypes")
- public void updated(Dictionary dictionary) throws ConfigurationException {
- //AMDATU-475 hotfix
- if(dictionary == null) {
- m_logService.log(LogService.LOG_ERROR, "I can't handle config
deletes yet!");
- return;
- }
- if (dictionary.get("workdir") == null) {
- throw new ConfigurationException("Missing configuration key",
"workdir");
- }
- m_workDirName = dictionary.get("workdir").toString();
- }
-}
+package org.amdatu.core.config.templates.service;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.StringWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.amdatu.core.config.templates.ConfigTemplateCallbackHandler;
+import org.amdatu.core.config.templates.ConfigTemplateManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.log.LogService;
+
+/**
+ * This class implements the Config Template Manager.
+ *
+ * @author <a href="mailto:[email protected]">Amdatu Project
Team</a>
+ */
+public class ConfigTemplateManagerImpl implements ConfigTemplateManager {
+ // 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\\.-_])+\\}");
+
+ // Prefix for System property replacement
+ private static final String SYSTEM_PROPERTY = "system";
+
+ // Services injected by the Felix dependency manager
+ private volatile BundleContext m_bundleContext;
+ private volatile LogService m_logService;
+ private volatile ConfigurationAdmin m_configurationAdmin;
+
+ // Our working directory
+ private File m_storageDir;
+
+ // 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>();
+
+ public synchronized void start() throws Exception {
+ File storageDir = m_bundleContext.getDataFile("");
+ if (storageDir == null) {
+ throw new Exception("Local storage not supported");
+ }
+ m_storageDir = storageDir;
+ m_logService.log(LogService.LOG_DEBUG, "Config Templates service
started");
+ }
+
+ public synchronized void stop() {
+ m_logService.log(LogService.LOG_DEBUG, "Config Templates service
stopped");
+ }
+
+ public synchronized URL getConfigurationAsURL(URL templateURL) throws
IOException {
+ if (m_urlCache.containsKey(templateURL.toString())) {
+ String cachedUrl = m_urlCache.get(templateURL.toString());
+ try {
+ return new URL(cachedUrl);
+ }
+ catch (MalformedURLException e) {
+ m_logService.log(LogService.LOG_ERROR, "Could not create URL
from cached URI'" + cachedUrl
+ + "'. Refreshing cache", e);
+ m_urlCache.remove(templateURL.toString());
+ }
+ }
+
+ // Get the temporary directory for web files
+ File configFile;
+ int urlId = getURLId();
+
+ // Determine the filename for the converted file
+ String filename = urlId + ".config";
+
+ // Create the temporary file to write the converted configuration
template to
+ configFile = new File(m_storageDir, filename);
+
+ // Write the file with replaced configuration entries to the target
file
+ writeConfiguration(templateURL, configFile);
+
+ // Return the new file URL
+ URL url = configFile.toURI().toURL();
+ m_urlCache.put(templateURL.toString(), url.toExternalForm());
+ return url;
+ }
+
+ /**
+ * @see
org.amdatu.core.config.templates.ConfigTemplateManager#getConfigurationAsString(java.net.URL)
+ */
+ public synchronized StringBuffer getConfigurationAsString(URL templateURL)
throws IOException {
+ // Write the file with replaced configuration entries to the target
file
+ BufferedWriter writer = null;
+ StringWriter sWiter = new StringWriter();
+ try {
+ writer = new BufferedWriter(sWiter);
+ writeConfiguration(templateURL, writer);
+ }
+ finally {
+ if (writer != null) {
+ writer.close();
+ }
+ }
+ return sWiter.getBuffer();
+ }
+
+ /**
+ * @see
org.amdatu.core.config.templates.ConfigTemplateManager#writeConfiguration(java.net.URL,
java.io.File)
+ */
+ public void writeConfiguration(URL templateURL, File targetFile) throws
IOException {
+ writeConfiguration(templateURL, targetFile, null);
+ }
+
+ public void writeConfiguration(URL templateURL, File targetFile,
ConfigTemplateCallbackHandler handler)
+ throws IOException {
+ if (!targetFile.exists()) {
+ if (targetFile.getParentFile() != null &&
!targetFile.getParentFile().exists()) {
+ if (!targetFile.getParentFile().mkdir()) {
+ throw new IOException("Could not create parent directory '"
+ + targetFile.getParentFile().getAbsolutePath() + "'");
+ }
+ }
+ if (!targetFile.createNewFile()) {
+ throw new IOException("Could not create file '" +
targetFile.getAbsolutePath() + "'");
+ }
+ }
+
+ // Now write the converted file to disk
+ BufferedWriter writer = null;
+ try {
+ writer = new BufferedWriter(new FileWriter(targetFile));
+ writeConfiguration(templateURL, writer, handler);
+ }
+ finally {
+ if (writer != null) {
+ writer.close();
+ }
+ }
+ }
+
+ /**
+ * @see
org.amdatu.core.config.templates.ConfigTemplateManager#writeConfiguration(java.net.URL,
java.io.BufferedWriter)
+ */
+ public void writeConfiguration(URL templateURL, BufferedWriter writer)
throws IOException {
+ writeConfiguration(templateURL, writer, null);
+ }
+
+ /**
+ * @param templateURL
+ * @param writer
+ * @param handler
+ * @throws IOException
+ */
+ public void writeConfiguration(URL templateURL, BufferedWriter writer,
ConfigTemplateCallbackHandler handler)
+ throws IOException {
+ // Now write the converted entries to the writer
+ BufferedReader reader = null;
+ try {
+ try {
+ URLConnection inputConnection = templateURL.openConnection();
+ reader = new BufferedReader(new
InputStreamReader(inputConnection.getInputStream()));
+ String inputLine;
+ String outputLine;
+ while ((inputLine = reader.readLine()) != null) {
+ outputLine = replaceConfigEntries(inputLine, handler);
+ writer.write(outputLine + "\r\n");
+ }
+ }
+ finally {
+ if (reader != null) {
+ reader.close();
+ }
+ }
+ }
+ finally {
+ if (writer != null) {
+ writer.close();
+ }
+ }
+ }
+
+ /**
+ * @see
org.amdatu.core.config.templates.ConfigTemplateManager#clear(java.lang.String)
+ */
+ public synchronized void clear(String templateURL) {
+ if (m_urlCache.containsKey(templateURL)) {
+ String url = m_urlCache.get(templateURL);
+ m_urlCache.remove(templateURL);
+ String configId = url.substring(url.lastIndexOf("/") + 1);
+ File file = new File(m_storageDir, configId);
+ if (file.exists()) {
+ if (!file.delete()) {
+ m_logService.log(LogService.LOG_INFO, "Failed to remove
file '" + file.getAbsolutePath()
+ + "' from cache");
+ }
+ }
+ }
+ }
+
+ /**
+ * Returns a guaranteed unique identifier to be used for the URL mapping
+ *
+ * @return Identifier to use for a URL mapping
+ */
+ private synchronized int getURLId() {
+ m_latestId++;
+ return m_latestId;
+ }
+
+ /**
+ * Replaces configuration entries in the line
+ *
+ * @param line
+ * @return
+ */
+ private String replaceConfigEntries(String line,
ConfigTemplateCallbackHandler handler) {
+ // For performance reasons, return immediately if the line does not
contain ${
+ if (line.indexOf("${") == -1) {
+ return line;
+ }
+
+ String newLine = line;
+ Matcher matcher = CONFIG_ENTRY_REGEX.matcher(line);
+ while (matcher.find()) {
+ String key = matcher.group();
+ String pid = key.substring("${".length(), key.indexOf("/"));
+ String configentry = key.substring(key.indexOf("/") + 1,
key.length() - 1);
+ Configuration config;
+ try {
+ config = m_configurationAdmin.getConfiguration(pid, null);
+ if (pid.equalsIgnoreCase(SYSTEM_PROPERTY)) {
+ // This is a system property, replace it
+ String value = System.getProperty(configentry);
+ if (value == null) {
+ value = "";
+ }
+ newLine = newLine.replace(key, value);
+ }
+ else if (config != null) {
+ Object entry = config.getProperties().get(configentry);
+ if (entry != null) {
+ String value = entry.toString();
+ if (handler != null) {
+ value = handler.getValue(pid, configentry, entry);
+ newLine = newLine.replace(key, value);
+ }
+ else {
+ newLine = newLine.replace(key, value);
+ }
+ }
+ }
+ }
+ catch (IOException e) {
+ m_logService.log(LogService.LOG_WARNING, "Configuration for
pid '" + pid
+ + "' does not exist, entry ignored", e);
+ }
+ }
+ return newLine;
+ }
+}
Modified: trunk/amdatu-release/src/main/resources/config/amdatu-core-config.xml
==============================================================================
--- trunk/amdatu-release/src/main/resources/config/amdatu-core-config.xml
(original)
+++ trunk/amdatu-release/src/main/resources/config/amdatu-core-config.xml
Tue Dec 13 13:01:59 2011
@@ -1,12 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0">
- <Designate pid="org.amdatu.core.config.templates">
- <Object ocdref="org.amdatu.core.config.templates">
- <Attribute adref="workdir">
- <Value>config-template-manager</Value>
- </Attribute>
- </Object>
- </Designate>
<Designate pid="org.amdatu.core.log.console">
<Object ocdref="org.amdatu.core.log.console">
<Attribute adref="loglevel">
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits