You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/10/02 13:07:06 UTC
svn commit: r701077 - in
/servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main:
Lock.java Main.java SimpleFileLock.java
Author: gnodet
Date: Thu Oct 2 04:07:05 2008
New Revision: 701077
URL: http://svn.apache.org/viewvc?rev=701077&view=rev
Log:
SMX4KNL-106: Support for container level lock to support master / slave deployments
Added:
servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Lock.java
servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/SimpleFileLock.java
Modified:
servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Main.java
Added: servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Lock.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Lock.java?rev=701077&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Lock.java (added)
+++ servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Lock.java Thu Oct 2 04:07:05 2008
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.servicemix.kernel.main;
+
+public interface Lock {
+
+ boolean lock() throws Exception;
+
+ void release() throws Exception;
+
+}
Modified: servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Main.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Main.java?rev=701077&r1=701076&r2=701077&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Main.java (original)
+++ servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/Main.java Thu Oct 2 04:07:05 2008
@@ -109,12 +109,25 @@
*/
public static final String PROPERTY_CONVERT_TO_MAVEN_URL = "servicemix.maven.convert";
+ /**
+ * If a lock should be used before starting the runtime
+ */
+ public static final String PROPERTY_USE_LOCK = "servicemix.lock";
+
+ /**
+ * The lock implementation
+ */
+ public static final String PROPERTY_LOCK_CLASS = "servicemix.lock.class";
+
+ public static final String PROPERTY_LOCK_CLASS_DEFAULT = SimpleFileLock.class.getName();
+
private File servicemixHome;
private File servicemixBase;
private static Properties m_configProps = null;
private static Felix m_felix = null;
private final String[] args;
private int exitCode;
+ private Lock lock;
public Main(String[] args) {
this.args = args;
@@ -266,6 +279,7 @@
activations.add(main);
try {
+ main.lock(m_configProps);
// Start up the OSGI framework
m_felix = new Felix(new StringMap(m_configProps, false), activations);
m_felix.start();
@@ -285,6 +299,7 @@
System.err.println("Error occured shutting down framework: " + ex);
ex.printStackTrace();
} finally {
+ main.unlock();
System.exit(main.getExitCode());
}
}
@@ -305,7 +320,7 @@
}
}
- // Try to figure it out using the jar file this class was loaded from.
+ // Try to figure it out using the jar file this class was loaded from.
if (rc == null) {
// guess the home from the location of the jar
URL url = Main.class.getClassLoader().getResource(Main.class.getName().replace(".", "/") + ".class");
@@ -782,6 +797,7 @@
e.hasMoreElements();) {
String key = (String) e.nextElement();
if (key.startsWith("felix.") ||
+ key.startsWith("servicemix.") ||
key.equals("org.osgi.framework.system.packages") ||
key.equals("org.osgi.framework.bootdelegation")) {
configProps.setProperty(key, System.getProperty(key));
@@ -1032,4 +1048,30 @@
public File getServicemixBase() {
return servicemixBase;
}
+
+ public void lock(Properties props) throws Exception {
+ if (Boolean.parseBoolean(props.getProperty(PROPERTY_USE_LOCK, "true"))) {
+ String clz = props.getProperty(PROPERTY_LOCK_CLASS, PROPERTY_LOCK_CLASS_DEFAULT);
+ lock = (Lock) Class.forName(clz).getConstructor(Properties.class).newInstance(props);
+ boolean lockLogged = false;
+ for (;;) {
+ if (lock.lock()) {
+ if (lockLogged) {
+ System.out.println("Lock acquired.");
+ }
+ break;
+ } else if (!lockLogged) {
+ System.out.println("Waiting for the lock ...");
+ lockLogged = true;
+ }
+ Thread.sleep(1000);
+ }
+ }
+ }
+
+ public void unlock() throws Exception {
+ if (lock != null) {
+ lock.release();
+ }
+ }
}
Added: servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/SimpleFileLock.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/SimpleFileLock.java?rev=701077&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/SimpleFileLock.java (added)
+++ servicemix/smx4/kernel/trunk/main/src/main/java/org/apache/servicemix/kernel/main/SimpleFileLock.java Thu Oct 2 04:07:05 2008
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.servicemix.kernel.main;
+
+import java.io.RandomAccessFile;
+import java.io.File;
+import java.io.IOException;
+import java.nio.channels.FileLock;
+import java.util.Properties;
+
+public class SimpleFileLock implements Lock {
+
+ private RandomAccessFile lockFile;
+ private FileLock lock;
+
+ public SimpleFileLock(Properties props) {
+ try {
+ File base = new File(props.getProperty("servicemix.base"));
+ lockFile = new RandomAccessFile(new File(base, "lock"), "rw");
+ } catch (IOException e) {
+ throw new RuntimeException("Could not create file lock", e);
+ }
+ }
+
+ public boolean lock() throws Exception {
+ if (lock == null) {
+ lock = lockFile.getChannel().tryLock();
+ }
+ return lock != null;
+ }
+
+ public void release() throws Exception {
+ if (lock != null && lock.isValid()) {
+ lock.release();
+ lock.channel().close();
+ }
+ lock = null;
+ }
+}