functionloader cleanup

This commit is contained in:
Adrian Cole 2012-06-08 13:00:38 -07:00
parent 5627ceb2b9
commit 73e026b314
14 changed files with 353 additions and 281 deletions

View File

@ -63,7 +63,6 @@ public class ClassLoadingUtils {
URL url = null;
if (contextClass != null) {
url = Resources.getResource(contextClass, resourceName);
}
if (url == null && Thread.currentThread().getContextClassLoader() != null) {
url = Thread.currentThread().getContextClassLoader().getResource(resourceName);

View File

@ -565,7 +565,6 @@ pageTracker._trackPageview();
<Import-Package>${jclouds.osgi.import}</Import-Package>
<DynamicImport-Package>${jclouds.osgi.dynamic}</DynamicImport-Package>
<Fragment-Host>${jclouds.osgi.fragment}</Fragment-Host>
<Bundle-Activator>${jclouds.osgi.activator}</Bundle-Activator>
</instructions>
</configuration>
</plugin>

View File

@ -61,4 +61,25 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<!-- ensure activator is set -->
<configuration>
<obrRepository>NONE</obrRepository>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Export-Package>${jclouds.osgi.export}</Export-Package>
<Import-Package>${jclouds.osgi.import}</Import-Package>
<DynamicImport-Package>${jclouds.osgi.dynamic}</DynamicImport-Package>
<Fragment-Host>${jclouds.osgi.fragment}</Fragment-Host>
<Bundle-Activator>${jclouds.osgi.activator}</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.scriptbuilder;
package org.jclouds.scriptbuilder.functionloader;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
@ -30,20 +30,25 @@ import java.io.IOException;
/**
* A {@link FunctionLoader} implementation which loads the target function from the classpath.
*/
public class BasicFunctionLoader implements FunctionLoader {
public enum BasicFunctionLoader implements FunctionLoader {
INSTANCE;
/**
* Loads a function from the classpath using the current or the Thread Context Class Loader.
* @param function The function name to load.
* @param family This operating system family of the function.
*
* @param function
* The function name to load.
* @param family
* This operating system family of the function.
* @return The function as {@link String}
* @throws FunctionNotFoundException
*/
@Override
public String loadFunction(String function, OsFamily family) throws FunctionNotFoundException {
try {
return CharStreams.toString(Resources.newReaderSupplier(ClassLoadingUtils.loadResource(BasicFunctionLoader.class, String
.format("/functions/%s.%s", function, ShellToken.SH.to(family))), Charsets.UTF_8));
return CharStreams.toString(Resources.newReaderSupplier(ClassLoadingUtils.loadResource(
BasicFunctionLoader.class, String.format("/functions/%s.%s", function, ShellToken.SH.to(family))),
Charsets.UTF_8));
} catch (IOException e) {
throw new FunctionNotFoundException(function, family, e);
}

View File

@ -0,0 +1,43 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.scriptbuilder.functionloader;
import java.util.concurrent.atomic.AtomicReference;
/**
* Means to access the current {@link FunctionLoader} instance;
*/
public class CurrentFunctionLoader {
private static final AtomicReference<FunctionLoader> ref = new AtomicReference<FunctionLoader>(
BasicFunctionLoader.INSTANCE);
public static FunctionLoader get() {
return ref.get();
}
public static FunctionLoader set(FunctionLoader loader) {
return ref.getAndSet(loader);
}
public static FunctionLoader reset() {
return ref.getAndSet(BasicFunctionLoader.INSTANCE);
}
}

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.scriptbuilder;
package org.jclouds.scriptbuilder.functionloader;
import org.jclouds.scriptbuilder.domain.OsFamily;

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.scriptbuilder;
package org.jclouds.scriptbuilder.functionloader;
import org.jclouds.scriptbuilder.domain.OsFamily;

View File

@ -0,0 +1,76 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.scriptbuilder.functionloader.osgi;
import org.jclouds.scriptbuilder.functionloader.CurrentFunctionLoader;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
BundleFunctionLoader functionLoader;
/**
* Called when this bundle is started so the Framework can perform the bundle-specific activities
* necessary to start this bundle. This method can be used to register services or to allocate
* any resources that this bundle needs.
* <p/>
* <p/>
* This method must complete and return to its caller in a timely manner.
*
* @param context
* The execution context of the bundle being started.
* @throws Exception
* If this method throws an exception, this bundle is marked as stopped and the
* Framework will remove this bundle's listeners, unregister all services registered
* by this bundle, and release all services used by this bundle.
*/
@Override
public void start(BundleContext context) throws Exception {
functionLoader = new BundleFunctionLoader(context);
functionLoader.start();
CurrentFunctionLoader.set(new ServiceFunctionLoader(context));
}
/**
* Called when this bundle is stopped so the Framework can perform the bundle-specific activities
* necessary to stop the bundle. In general, this method should undo the work that the
* <code>BundleActivator.start</code> method started. There should be no active threads that were
* started by this bundle when this bundle returns. A stopped bundle must not call any Framework
* objects.
* <p/>
* <p/>
* This method must complete and return to its caller in a timely manner.
*
* @param context
* The execution context of the bundle being stopped.
* @throws Exception
* If this method throws an exception, the bundle is still marked as stopped, and the
* Framework will remove the bundle's listeners, unregister all services registered by
* the bundle, and release all services used by the bundle.
*/
@Override
public void stop(BundleContext context) throws Exception {
if (functionLoader != null) {
CurrentFunctionLoader.reset();
functionLoader.stop();
functionLoader = null;
}
}
}

View File

@ -0,0 +1,109 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.scriptbuilder.functionloader.osgi;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.ShellToken;
import org.jclouds.scriptbuilder.functionloader.FunctionLoader;
import org.jclouds.scriptbuilder.functionloader.FunctionNotFoundException;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;
/**
* A {@link FunctionLoader} which searches for functions in the {@link Bundle} resources.
*/
public class BundleFunctionLoader implements FunctionLoader {
private final BundleContext bundleContext;
private ServiceRegistration registration;
/**
* Constructor
*
* @param bundleContext
*/
public BundleFunctionLoader(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
/**
* Starts the loader. Looks up for {@link Bundle} resources and registers itself in the service
* registry. It adds a property to the service which advertise all the functions found in the
* local resources.
*/
public void start() {
Bundle bundle = bundleContext.getBundle();
Enumeration<?> entries = bundle.findEntries("/functions/", "*.*", false);
StringBuilder sb = new StringBuilder();
while (entries.hasMoreElements()) {
URL url = (URL) entries.nextElement();
String function = url.getFile();
sb.append(function);
if (entries.hasMoreElements()) {
sb.append(" ");
}
}
String functions = sb.toString();
registerFunction(functions);
}
/**
* Unregisters itself from the service registry.
*/
public void stop() {
registration.unregister();
}
/**
* Loads the function from the {@link Bundle} resources.
*
* @param function
* The function name to load.
* @param family
* This operating system family of the function.
* @return
* @throws FunctionNotFoundException
*/
@Override
public String loadFunction(String function, OsFamily family) throws FunctionNotFoundException {
try {
return CharStreams.toString(Resources.newReaderSupplier(bundleContext.getBundle().getResource(
String.format("/functions/%s.%s", function, ShellToken.SH.to(family))), Charsets.UTF_8));
} catch (IOException e) {
throw new FunctionNotFoundException(function, family, e);
}
}
private void registerFunction(String functions) {
Properties properties = new Properties();
properties.put("function", functions);
registration = bundleContext.registerService(FunctionLoader.class.getName(), this, properties);
}
}

View File

@ -0,0 +1,77 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.scriptbuilder.functionloader.osgi;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.ShellToken;
import org.jclouds.scriptbuilder.functionloader.FunctionLoader;
import org.jclouds.scriptbuilder.functionloader.FunctionNotFoundException;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import com.google.common.base.Strings;
/**
* A {@link FunctionLoader} implementation which delegates loading to the OSGi service registry.
*/
public class ServiceFunctionLoader implements FunctionLoader {
private final BundleContext bundleContext;
public ServiceFunctionLoader(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
/**
* Looks up the service registry for an applicable {@link FunctionLoader} and delegates to it.
*
* @param function
* The function name to load.
* @param family
* This operating system family of the function.
* @return The function as {@link String}
* @throws FunctionNotFoundException
*/
@Override
public String loadFunction(String function, OsFamily family) throws FunctionNotFoundException {
ServiceReference[] references = null;
String filter = String.format("(function=*%s.%s*)", function, ShellToken.SH.to(family));
try {
references = bundleContext.getServiceReferences(FunctionLoader.class.getName(), filter);
for (ServiceReference reference : references) {
FunctionLoader loader = (FunctionLoader) bundleContext.getService(reference);
String f = loader.loadFunction(function, family);
if (!Strings.isNullOrEmpty(f)) {
return f;
}
}
} catch (InvalidSyntaxException e) {
throw new FunctionNotFoundException(function, family, e);
} finally {
if (references != null) {
for (ServiceReference reference : references) {
bundleContext.ungetService(reference);
}
}
}
throw new FunctionNotFoundException(function, family);
}
}

View File

@ -1,76 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.scriptbuilder.osgi;
import org.jclouds.scriptbuilder.util.Utils;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
BundleFunctionLoader functionLoader;
/**
* Called when this bundle is started so the Framework can perform the
* bundle-specific activities necessary to start this bundle. This method
* can be used to register services or to allocate any resources that this
* bundle needs.
* <p/>
* <p/>
* This method must complete and return to its caller in a timely manner.
*
* @param context The execution context of the bundle being started.
* @throws Exception If this method throws an exception, this
* bundle is marked as stopped and the Framework will remove this
* bundle's listeners, unregister all services registered by this
* bundle, and release all services used by this bundle.
*/
@Override
public void start(BundleContext context) throws Exception {
functionLoader = new BundleFunctionLoader(context);
functionLoader.start();
Utils.functionLoader = new ServiceFunctionLoader(context);
}
/**
* Called when this bundle is stopped so the Framework can perform the
* bundle-specific activities necessary to stop the bundle. In general, this
* method should undo the work that the <code>BundleActivator.start</code>
* method started. There should be no active threads that were started by
* this bundle when this bundle returns. A stopped bundle must not call any
* Framework objects.
* <p/>
* <p/>
* This method must complete and return to its caller in a timely manner.
*
* @param context The execution context of the bundle being stopped.
* @throws Exception If this method throws an exception, the
* bundle is still marked as stopped, and the Framework will remove
* the bundle's listeners, unregister all services registered by the
* bundle, and release all services used by the bundle.
*/
@Override
public void stop(BundleContext context) throws Exception {
if (functionLoader != null) {
functionLoader.stop();
functionLoader = null;
}
}
}

View File

@ -1,105 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.scriptbuilder.osgi;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;
import org.jclouds.scriptbuilder.FunctionLoader;
import org.jclouds.scriptbuilder.FunctionNotFoundException;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.ShellToken;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
/**
* A {@link FunctionLoader} which searches for functions in the {@link Bundle} resources.
*/
public class BundleFunctionLoader implements FunctionLoader {
private final BundleContext bundleContext;
private ServiceRegistration registration;
/**
* Constructor
*
* @param bundleContext
*/
public BundleFunctionLoader(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
/**
* Starts the loader.
* Looks up for {@link Bundle} resources and registers itself in the service registry.
* It adds a property to the service which advertise all the functions found in the local resources.
*/
public void start() {
Bundle bundle = bundleContext.getBundle();
Enumeration entries = bundle.findEntries("/functions/", "*.*", false);
StringBuilder sb = new StringBuilder();
while (entries.hasMoreElements()) {
URL url = (URL) entries.nextElement();
String function = url.getFile();
sb.append(function);
if (entries.hasMoreElements()) {
sb.append(" ");
}
}
String functions = sb.toString();
registerFunction(functions);
}
/**
* Unregisters itself from the service registry.
*/
public void stop() {
registration.unregister();
}
/**
* Loads the function from the {@link Bundle} resources.
* @param function The function name to load.
* @param family This operating system family of the function.
* @return
* @throws FunctionNotFoundException
*/
@Override
public String loadFunction(String function, OsFamily family) throws FunctionNotFoundException {
try {
return CharStreams.toString(Resources.newReaderSupplier(bundleContext.getBundle().getResource(String.format("/functions/%s.%s", function, ShellToken.SH.to(family))), Charsets.UTF_8));
} catch (IOException e) {
throw new FunctionNotFoundException(function, family, e);
}
}
private void registerFunction(String functions) {
Properties properties = new Properties();
properties.put("function",functions);
registration = bundleContext.registerService(FunctionLoader.class.getName(), this,properties);
}
}

View File

@ -1,73 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.jclouds.scriptbuilder.osgi;
import com.google.common.base.Strings;
import org.jclouds.scriptbuilder.FunctionLoader;
import org.jclouds.scriptbuilder.FunctionNotFoundException;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.ShellToken;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
/**
* A {@link FunctionLoader} implementation which delegates loading to the OSGi service registry.
*/
public class ServiceFunctionLoader implements FunctionLoader {
private final BundleContext bundleContext;
public ServiceFunctionLoader(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
/**
* Looks up the service registry for an applicable {@link FunctionLoader} and delegates to it.
* @param function The function name to load.
* @param family This operating system family of the function.
* @return The function as {@link String}
* @throws FunctionNotFoundException
*/
@Override
public String loadFunction(String function, OsFamily family) throws FunctionNotFoundException {
ServiceReference[] references = null;
String filter = String.format("(function=*%s.%s*)", function, ShellToken.SH.to(family));
try {
references = bundleContext.getServiceReferences(FunctionLoader.class.getName(), filter);
for (ServiceReference reference : references) {
FunctionLoader loader = (FunctionLoader) bundleContext.getService(reference);
String f = loader.loadFunction(function, family);
if (!Strings.isNullOrEmpty(f)) {
return f;
}
}
} catch (InvalidSyntaxException e) {
throw new FunctionNotFoundException(function, family, e);
} finally {
if (references != null) {
for (ServiceReference reference : references) {
bundleContext.ungetService(reference);
}
}
}
throw new FunctionNotFoundException(function, family);
}
}

View File

@ -24,10 +24,9 @@ import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jclouds.scriptbuilder.BasicFunctionLoader;
import org.jclouds.scriptbuilder.FunctionLoader;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.ShellToken;
import org.jclouds.scriptbuilder.functionloader.CurrentFunctionLoader;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
@ -44,8 +43,6 @@ public class Utils {
public static final LowerCamelToUpperUnderscore FUNCTION_LOWER_CAMEL_TO_UPPER_UNDERSCORE = new LowerCamelToUpperUnderscore();
public static FunctionLoader functionLoader = new BasicFunctionLoader();
public static final class LowerCamelToUpperUnderscore implements Function<String, String> {
@Override
public String apply(String from) {
@ -134,7 +131,7 @@ public class Utils {
}
public static String writeFunctionFromResource(String function, OsFamily family) {
String toReturn = functionLoader.loadFunction(function,family);
String toReturn = CurrentFunctionLoader.get().loadFunction(function,family);
String lf = ShellToken.LF.to(family);
return toReturn.endsWith(lf) ? toReturn : new StringBuilder(toReturn).append(lf).toString();
}