Author: [email protected]
Date: Tue Mar 15 17:14:02 2011
New Revision: 888
Log:
AMDATU-258 moved war support classes from release module to seperate library
Added:
trunk/amdatu-libraries/warsupport/ (props changed)
trunk/amdatu-libraries/warsupport/pom.xml
trunk/amdatu-libraries/warsupport/src/
trunk/amdatu-libraries/warsupport/src/main/
trunk/amdatu-libraries/warsupport/src/main/java/
trunk/amdatu-libraries/warsupport/src/main/java/org/
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/FrameworkService.java
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/ProvisionActivator.java
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/StartupListener.java
Modified:
trunk/amdatu-libraries/pom.xml
Modified: trunk/amdatu-libraries/pom.xml
==============================================================================
--- trunk/amdatu-libraries/pom.xml (original)
+++ trunk/amdatu-libraries/pom.xml Tue Mar 15 17:14:02 2011
@@ -15,6 +15,7 @@
<modules>
<module>utilities</module>
<module>fsstorage</module>
+ <module>warsupport</module>
</modules>
</project>
\ No newline at end of file
Added: trunk/amdatu-libraries/warsupport/pom.xml
==============================================================================
--- (empty file)
+++ trunk/amdatu-libraries/warsupport/pom.xml Tue Mar 15 17:14:02 2011
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.amdatu</groupId>
+ <artifactId>org.amdatu.libraries</artifactId>
+ <version>0.2.0-SNAPSHOT</version>
+ </parent>
+ <groupId>org.amdatu.libraries</groupId>
+ <artifactId>warsupport</artifactId>
+ <packaging>jar</packaging>
+ <name>Amdatu Libraries - War support</name>
+ <description>Supporting classes for the Amdatu WAR assembly</description>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.framework</artifactId>
+ <version>${org.apache.felix.main.version}</version>
+ </dependency>
+ </dependencies>
+ <build>
+
<finalName>${project.groupId}.${project.artifactId}-${project.version}</finalName>
+ </build>
+</project>
Added:
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/FrameworkService.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/FrameworkService.java
Tue Mar 15 17:14:02 2011
@@ -0,0 +1,164 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.webapp;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.servlet.ServletContext;
+
+import org.apache.felix.framework.Felix;
+import org.apache.felix.framework.util.FelixConstants;
+
+public final class FrameworkService {
+ private static final String AMDATU_DIR = "amdatu.dir";
+
+ private final ServletContext m_servletContext;
+ private Felix m_felix;
+
+ public FrameworkService(ServletContext context) {
+ m_servletContext = context;
+ }
+
+ public void start() {
+ try {
+ doStart();
+ } catch (Exception e) {
+ log("Failed to start framework", e);
+ }
+ }
+
+ public void stop() {
+ try {
+ doStop();
+ } catch (Exception e) {
+ log("Error stopping framework", e);
+ }
+ }
+
+ private void doStart() throws Exception {
+ System.out.println("Starting Felix");
+ Felix tmp = new Felix(createConfig());
+ tmp.start();
+ m_felix = tmp;
+ log("OSGi framework started", null);
+ }
+
+ private void doStop() throws Exception {
+ if (m_felix != null) {
+ m_felix.stop();
+ }
+
+ log("OSGi framework stopped", null);
+ }
+
+ private Map<String, Object> createConfig() throws Exception {
+ // Load and set framework properties
+ Properties props = new Properties();
+
props.load(m_servletContext.getResourceAsStream("/WEB-INF/framework.properties"));
+ HashMap<String, Object> map = new HashMap<String, Object>();
+ for (Object key : props.keySet()) {
+ map.put(key.toString(), props.get(key));
+ }
+
+ // Check if a system property amdatu.dir was provided. If not, set the
current directory
+ // as amdatu.dir. The amdatu.dir is used by all Amdatu components that
use relative
+ // directories for file storage (i.e. for Cassandra commitlog files
and felix-deploy cache).
+ String amdatuDir = System.getProperty(AMDATU_DIR);
+ if (amdatuDir == null || amdatuDir.isEmpty()) {
+ // amdatu.dir was not provided as system property, maybe as
framework property?
+ if (map.containsKey(AMDATU_DIR)) {
+ amdatuDir = map.get(AMDATU_DIR).toString();
+ System.out.println(AMDATU_DIR + "=" + amdatuDir + " (provided
as framework property)");
+ } else {
+ amdatuDir = System.getProperty("user.dir");
+ System.out.println(AMDATU_DIR + "=" + amdatuDir + " (set to
current working directory)");
+ }
+ } else {
+ System.out.println(AMDATU_DIR + "=" + amdatuDir + " (provided as
system property)");
+ }
+ System.setProperty(AMDATU_DIR, amdatuDir);
+
+ // Ensure that the Amdatu directory exists
+ new File(amdatuDir).mkdirs();
+
+ // Convert the relative paths in framework.properties to absolute
paths by prefixing it
+ // with the AMDATU_WORK_DIR
+ System.out.println("Setting framework directory properties:");
+ relativeToAbsolute(amdatuDir, map, "felix.cache.rootdir");
+ relativeToAbsolute(amdatuDir, map, "felix.cm.dir");
+ relativeToAbsolute(amdatuDir, map, "felix.fileinstall.dir");
+ relativeToAbsolute(amdatuDir, map, "felix.fileinstall.tmpdir");
+
+ // Now when Amdatu is booted for the very first time, the target
deploy directory used
+ // by fileinstall will be empty. So we must copy the files included in
the WAR to this
+ // directory
+ File deployDir = new File(map.get("felix.fileinstall.dir").toString());
+ if (!deployDir.exists()) {
+ deployDir.mkdirs();
+ String sourceDir = m_servletContext.getRealPath("/amdatu/deploy");
+ System.out.println("Config Admin deploy directory does not exist,
copying initial configuration files from "
+ + sourceDir + " to " + deployDir.getAbsolutePath());
+ String[] files = new File(sourceDir).list();
+ for (String file : files) {
+ String target = deployDir.getAbsolutePath() + File.separator +
new File(file).getName();
+ copyFile(new File(sourceDir + File.separator + file), new
File(target));
+ }
+ }
+
+ map.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, Arrays.asList(new
ProvisionActivator(m_servletContext)));
+ return map;
+ }
+
+ private void relativeToAbsolute(String amdatuDir, Map<String, Object> map
, String propName) {
+ String newDir = amdatuDir + File.separator +
map.get(propName).toString();
+ map.put(propName, newDir);
+ System.out.println(" " + propName + "=" + newDir);
+ }
+
+ private void log(String message, Throwable cause) {
+ m_servletContext.log(message, cause);
+ }
+
+ private void copyFile(File source, File target) throws IOException {
+ InputStream in = null;
+ OutputStream out = null;
+ try {
+ in = new FileInputStream(source);
+ out = new FileOutputStream(target);
+ byte[] buf = new byte[1024];
+ int len;
+ while ((len = in.read(buf)) > 0) {
+ out.write(buf, 0, len);
+ }
+ } finally {
+ try {
+ if (in != null) in.close();
+ } finally {
+ if (out != null) out.close();
+ }
+ }
+ }
+}
Added:
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/ProvisionActivator.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/ProvisionActivator.java
Tue Mar 15 17:14:02 2011
@@ -0,0 +1,78 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.webapp;
+
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.ServletContext;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public final class ProvisionActivator implements BundleActivator {
+ private final ServletContext m_servletContext;
+
+ public ProvisionActivator(ServletContext servletContext) {
+ m_servletContext = servletContext;
+ }
+
+ public void start(BundleContext context) throws Exception {
+ System.out.println("Provisioning OSGi bundles");
+ m_servletContext.setAttribute(BundleContext.class.getName(), context);
+ ArrayList<Bundle> installed = new ArrayList<Bundle>();
+ for (URL url : findBundles()) {
+ this.m_servletContext.log("Installing bundle [" + url + "]");
+ System.out.println("Installing bundle [" + url + "]");
+ Bundle bundle = context.installBundle(url.toExternalForm());
+ installed.add(bundle);
+ }
+
+ for (Bundle bundle : installed) {
+ bundle.start();
+ }
+ }
+
+ public void stop(BundleContext context) throws Exception{
+ }
+
+ private List<URL> findBundles() throws Exception {
+ ArrayList<URL> allBundles = new ArrayList<URL>();
+ allBundles.addAll(loadDir("amdatu-system"));
+ allBundles.addAll(loadDir("amdatu-core"));
+ allBundles.addAll(loadDir("amdatu-application"));
+ allBundles.addAll(loadDir("amdatu-examples"));
+ return allBundles;
+ }
+
+ private List<URL> loadDir(String dirname) throws Exception{
+ ArrayList<URL> list = new ArrayList<URL>();
+ for (Object o : this.m_servletContext.getResourcePaths("/amdatu/" +
dirname + "/")) {
+ String name = (String) o;
+ if (name.endsWith(".jar")) {
+ URL url = this.m_servletContext.getResource(name);
+ if (url != null) {
+ list.add(url);
+ }
+ }
+ }
+ return list;
+ }
+}
Added:
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/StartupListener.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-libraries/warsupport/src/main/java/org/amdatu/webapp/StartupListener.java
Tue Mar 15 17:14:02 2011
@@ -0,0 +1,39 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.webapp;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+public final class StartupListener implements ServletContextListener {
+ private FrameworkService m_frameworkService;
+
+ public void contextInitialized(ServletContextEvent event) {
+ // FIXME: Context path is set here to a System property since Amdatu
components need it that
+ // do not have access to a HTTP request. Some application context is
needed here, this is
+ // covered by http://jira.amdatu.org/jira/browse/AMDATU-325
+ String contextPath = event.getServletContext().getContextPath();
+ System.setProperty("amdatu.contextpath", contextPath);
+
+ m_frameworkService = new FrameworkService(event.getServletContext());
+ m_frameworkService.start();
+ }
+
+ public void contextDestroyed(ServletContextEvent event) {
+ m_frameworkService.stop();
+ }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits