killbill-memoizeit
Details
diff --git a/api/src/main/java/com/ning/billing/lifecycle/IService.java b/api/src/main/java/com/ning/billing/lifecycle/IService.java
index 2e4a27f..4e4d6d1 100644
--- a/api/src/main/java/com/ning/billing/lifecycle/IService.java
+++ b/api/src/main/java/com/ning/billing/lifecycle/IService.java
@@ -45,30 +45,5 @@ public interface IService {
*/
public String getName();
- /**
- *
- * @throws ServiceException
- *
- * Initialize the service prior to start
- */
- public void initialize()
- throws ServiceException;
-
- /**
- *
- * @throws ServiceException
- *
- * Start the given service
- */
- public void start()
- throws ServiceException;
-
- /**
- * Stop the given service
- *
- * @throws ServiceException
- */
- public void stop()
- throws ServiceException;
}
diff --git a/api/src/main/java/com/ning/billing/lifecycle/Lifecycled.java b/api/src/main/java/com/ning/billing/lifecycle/Lifecycled.java
new file mode 100644
index 0000000..df1fb82
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/lifecycle/Lifecycled.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.lifecycle;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface Lifecycled {
+}
beatrix/pom.xml 12(+12 -0)
diff --git a/beatrix/pom.xml b/beatrix/pom.xml
index dee6794..6dc7bfc 100644
--- a/beatrix/pom.xml
+++ b/beatrix/pom.xml
@@ -25,6 +25,18 @@
<artifactId>killbill-api</artifactId>
</dependency>
<dependency>
+ <groupId>com.ning.billing</groupId>
+ <artifactId>killbill-entitlement</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.ning.billing</groupId>
+ <artifactId>killbill-catalog</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </dependency>
+ <dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
</dependency>
diff --git a/beatrix/src/main/java/com/ning/billing/beatrix/lifecycle/Lifecycle.java b/beatrix/src/main/java/com/ning/billing/beatrix/lifecycle/Lifecycle.java
index 32e9c42..ca8e994 100644
--- a/beatrix/src/main/java/com/ning/billing/beatrix/lifecycle/Lifecycle.java
+++ b/beatrix/src/main/java/com/ning/billing/beatrix/lifecycle/Lifecycle.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.beatrix.lifecycle;
import java.lang.reflect.Method;
@@ -21,38 +37,24 @@ import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.inject.Inject;
import com.google.inject.Injector;
-import com.ning.billing.beatrix.lifecycle.LyfecycleHandlerType.LyfecycleLevel;
+import com.ning.billing.lifecycle.IService;
+import com.ning.billing.lifecycle.LyfecycleHandlerType;
+import com.ning.billing.lifecycle.LyfecycleHandlerType.LyfecycleLevel;
public class Lifecycle {
private final static Logger log = LoggerFactory.getLogger(Lifecycle.class);
-
-
- private final class LifecycleHandler {
- private final Object target;
- private final Method method;
-
- public LifecycleHandler(Object target, Method method) {
- this.target = target;
- this.method = method;
- }
-
- public Object getTarget() {
- return target;
- }
-
- public Method getMethod() {
- return method;
- }
- }
-
private final SetMultimap<LyfecycleLevel, LifecycleHandler> handlersByLevel;
+ private final ServiceFinder serviceFinder;
+
private final Injector injector;
@Inject
public Lifecycle(Injector injector) {
+
+ this.serviceFinder = new ServiceFinder(Lifecycle.class.getClassLoader());
this.handlersByLevel = Multimaps.newSetMultimap(new ConcurrentHashMap<LyfecycleLevel, Collection<LifecycleHandler>>(),
new Supplier<Set<LifecycleHandler>>() {
@@ -65,57 +67,55 @@ public class Lifecycle {
}
public void init() {
- List<? extends LifecycleService> services = findServices();
- Iterator<? extends LifecycleService> it = services.iterator();
+ Set<? extends IService> services = findServices();
+ Iterator<? extends IService> it = services.iterator();
while (it.hasNext()) {
- //for (<? extends LifecycleService> cur : services) {
handlersByLevel.putAll(findAllHandlers(it.next()));
}
}
-
-
public void fireStages() {
for (LyfecycleLevel level : LyfecycleLevel.values()) {
log.info("Firing stage {}", level);
Set<LifecycleHandler> handlers = handlersByLevel.get(level);
for (LifecycleHandler cur : handlers) {
- log.info("Calling handler {}", cur.getMethod().getName());
+ log.debug("Calling handler {}", cur.getMethod().getName());
try {
Method method = cur.getMethod();
Object target = cur.getTarget();
method.invoke(target);
} catch (Exception e) {
- log.warn("Faikled to invoke lifecycle handler", e);
+ log.warn("Failed to invoke lifecycle handler", e);
}
-
}
}
}
- private List<? extends LifecycleService> findServices() {
-
- List<LifecycleService> result = new LinkedList<LifecycleService>();
+ private Set<? extends IService> findServices() {
+
+ Set<IService> result = new TreeSet<IService>();
+ Set<Class<? extends IService>> services = serviceFinder.getServices();
+ for (Class<? extends IService> cur : services) {
+ log.debug("Found service {}", cur);
+ try {
+ IService service = injector.getInstance(cur);
+ result.add(injector.getInstance(cur));
+ } catch (Exception e) {
+ log.warn("Failed to inject {}", cur.getName());
+ }
- ClassFinder classFinder = new ClassFinder(Lifecycle.class.getClassLoader());
- List<Class<? extends LifecycleService>> services = classFinder.getServices();
- for (Class<? extends LifecycleService> cur : services) {
- log.info("Found service {}", cur);
- result.add(injector.getInstance(cur));
}
return result;
}
- public Multimap<LyfecycleLevel, LifecycleHandler> findAllHandlers(Object service) {
+ public Multimap<LyfecycleLevel, LifecycleHandler> findAllHandlers(IService service) {
Multimap<LyfecycleLevel, LifecycleHandler> methodsInService =
HashMultimap.create();
- Class clazz = service.getClass();
+ Class<? extends IService> clazz = service.getClass();
for (Method method : clazz.getMethods()) {
LyfecycleHandlerType annotation = method.getAnnotation(LyfecycleHandlerType.class);
-
if (annotation != null) {
-
LyfecycleLevel level = annotation.value();
LifecycleHandler handler = new LifecycleHandler(service, method);
methodsInService.put(level, handler);
@@ -125,4 +125,21 @@ public class Lifecycle {
}
+ private final class LifecycleHandler {
+ private final Object target;
+ private final Method method;
+
+ public LifecycleHandler(Object target, Method method) {
+ this.target = target;
+ this.method = method;
+ }
+
+ public Object getTarget() {
+ return target;
+ }
+
+ public Method getMethod() {
+ return method;
+ }
+ }
}
diff --git a/beatrix/src/main/java/com/ning/billing/beatrix/lifecycle/ServiceFinder.java b/beatrix/src/main/java/com/ning/billing/beatrix/lifecycle/ServiceFinder.java
new file mode 100644
index 0000000..ffaf641
--- /dev/null
+++ b/beatrix/src/main/java/com/ning/billing/beatrix/lifecycle/ServiceFinder.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.beatrix.lifecycle;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Modifier;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.jar.JarFile;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.ning.billing.lifecycle.IService;
+
+public class ServiceFinder {
+
+ private final Logger log = LoggerFactory.getLogger(ServiceFinder.class);
+
+ private final ClassLoader loader;
+ private final Set<Class<? extends IService>> servicesTypes;
+
+ public ServiceFinder(ClassLoader loader) {
+ this.loader = loader;
+ this.servicesTypes = initialize();
+ Iterator<Class<? extends IService>> it = servicesTypes.iterator();
+ while (it.hasNext()) {
+ Class<? extends IService> svc = it.next();
+ log.debug("Found IService classes {}", svc.getName());
+ }
+ }
+
+ public Set<Class<? extends IService>> getServices() {
+ return servicesTypes;
+ }
+
+ private Set<Class<? extends IService>> initialize() {
+ try {
+
+ final Set<String> packageFilter = new HashSet<String>();
+ packageFilter.add("com.ning.billing");
+ final String jarFilter = "killbill";
+ return findClasses(loader, IService.class.getName().toString(), jarFilter, packageFilter);
+ } catch (ClassNotFoundException nfe) {
+ throw new RuntimeException("Failed to initialize ClassFinder", nfe);
+ }
+ }
+
+ /*
+ * Code originally from Kris Dover <krisdover@hotmail.com> and adapated for my purpose
+ *
+ */
+ private static Set<Class<? extends IService>> findClasses(ClassLoader classLoader,
+ String interfaceFilter,
+ String jarFilter,
+ Set<String> packageFilter)
+ throws ClassNotFoundException {
+
+ final Set<Class<? extends IService>> result = new HashSet<Class<? extends IService>>();
+
+ Object[] classPaths;
+ try {
+ classPaths = ((java.net.URLClassLoader) classLoader).getURLs();
+ } catch(ClassCastException cce){
+ classPaths = System.getProperty("java.class.path", "").split(File.pathSeparator);
+ }
+
+ for (int h = 0; h < classPaths.length; h++) {
+ Enumeration<?> files = null;
+ JarFile module = null;
+ File classPath = new File( (URL.class).isInstance(classPaths[h]) ?
+ ((URL)classPaths[h]).getFile() : classPaths[h].toString());
+
+ if (classPath.isDirectory()) {
+ List<String> dirListing = new ArrayList<String>();
+ recursivelyListDir(dirListing, classPath, new StringBuffer() );
+ files = Collections.enumeration( dirListing );
+ } else if (classPath.getName().endsWith(".jar")) {
+ String [] jarParts = classPath.getName().split("/");
+ String jarName = jarParts[jarParts.length - 1];
+ if (jarFilter != null && jarName != null && ! jarName.startsWith(jarFilter)) {
+ continue;
+ }
+ boolean failed = true;
+ try {
+ module = new JarFile(classPath);
+ failed = false;
+ } catch (MalformedURLException mue){
+ throw new ClassNotFoundException("Bad classpath. Error: " + mue.getMessage());
+ } catch (IOException io){
+ throw new ClassNotFoundException("jar file '" + classPath.getName() +
+ "' could not be instantiate from file path. Error: " + io.getMessage());
+ }
+ if (! failed) {
+ files = module.entries();
+ }
+ }
+
+ while( files != null && files.hasMoreElements() ){
+ String fileName = files.nextElement().toString();
+
+ if( fileName.endsWith(".class") ){
+ String className = fileName.replaceAll("/", ".").substring(0, fileName.length() - 6);
+ if (packageFilter != null) {
+ boolean skip = true;
+ Iterator<String> it = packageFilter.iterator();
+ while (it.hasNext()) {
+ String filter = it.next() + ".";
+ if (className.startsWith(filter)) {
+ skip = false;
+ break;
+ }
+ }
+ if (skip) {
+ continue;
+ }
+ }
+ Class<?> theClass = null;
+ try {
+ theClass = Class.forName(className, false, classLoader);
+ } catch(NoClassDefFoundError e) {
+ continue;
+ }
+ if ( theClass.isInterface() ) {
+ continue;
+ }
+ Class<?> [] classInterfaces = getAllInterfaces(theClass);
+ String interfaceName = null;
+ for (int i = 0; i < classInterfaces.length; i++) {
+
+ interfaceName = classInterfaces[i].getName();
+ if (!interfaceFilter.equals(interfaceName) ) {
+ continue;
+ }
+ result.add((Class<? extends IService>) theClass);
+ break;
+ }
+
+ }
+ }
+ if (module != null) {
+ try {
+ module.close();
+ } catch(IOException ioe) {
+ throw new ClassNotFoundException("The module jar file '" + classPath.getName() +
+ "' could not be closed. Error: " + ioe.getMessage());
+ }
+ }
+ }
+ return result;
+ }
+
+ private static Class<?> [] getAllInterfaces(Class<?> theClass) {
+ Set<Class<?>> superInterfaces = new HashSet<Class<?>>();
+ Class<?> [] classInterfaces = theClass.getInterfaces();
+ for (Class<?> cur : classInterfaces) {
+ getSuperInterfaces(superInterfaces, cur);
+ }
+ return superInterfaces.toArray(new Class<?>[superInterfaces.size()]);
+ }
+
+ private static void getSuperInterfaces(Set<Class<?>> superInterfaces, Class<?> theInterface) {
+
+ superInterfaces.add(theInterface);
+ Class<?> [] classInterfaces = theInterface.getInterfaces();
+ for (Class<?> cur : classInterfaces) {
+ getSuperInterfaces(superInterfaces, cur);
+ }
+ }
+
+ private static void recursivelyListDir(List<String> dirListing, File dir, StringBuffer relativePath){
+ int prevLen;
+ if (dir.isDirectory()) {
+ File[] files = dir.listFiles();
+ for(int i = 0; i < files.length; i++){
+ prevLen = relativePath.length();
+ recursivelyListDir(dirListing, files[i],
+ relativePath.append(prevLen == 0 ? "" : "/" ).append( files[i].getName()));
+ relativePath.delete(prevLen, relativePath.length());
+ }
+ } else {
+ dirListing.add(relativePath.toString());
+ }
+ }
+}
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/lifecycle/TestLifecycle.java b/beatrix/src/test/java/com/ning/billing/beatrix/lifecycle/TestLifecycle.java
index 269a13b..9314368 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/lifecycle/TestLifecycle.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/lifecycle/TestLifecycle.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.beatrix.lifecycle;
import java.util.HashSet;
@@ -13,8 +29,11 @@ import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Stage;
-import com.ning.billing.beatrix.lifecycle.LyfecycleHandlerType.LyfecycleLevel;
import com.ning.billing.entitlement.IEntitlementService;
+import com.ning.billing.lifecycle.IService;
+import com.ning.billing.lifecycle.Lifecycled;
+import com.ning.billing.lifecycle.LyfecycleHandlerType;
+import com.ning.billing.lifecycle.LyfecycleHandlerType.LyfecycleLevel;
public class TestLifecycle {
@@ -22,7 +41,7 @@ public class TestLifecycle {
private final static Logger log = LoggerFactory.getLogger(TestLifecycle.class);
@Lifecycled
- public static class Service1 implements LifecycleService {
+ public static class Service1 implements IService {
@LyfecycleHandlerType(LyfecycleLevel.INIT_BUS)
public void initBus() {
@@ -33,15 +52,26 @@ public class TestLifecycle {
public void startService() {
log.info("Service1 : got START_SERVICE");
}
+
+ @Override
+ public String getName() {
+ return null;
+ }
}
@Lifecycled
- public static class Service2 implements LifecycleService {
+ public static class Service2 implements IService {
@LyfecycleHandlerType(LyfecycleLevel.LOAD_CATALOG)
- public void initBus() {
- log.info("Service1 : got INIT_BUS");
+ public void loadCatalog() {
+ log.info("Service1 : got LOAD_CATALOG");
+ }
+
+ @Override
+ public String getName() {
+ return null;
}
+
}
@@ -61,7 +91,6 @@ public class TestLifecycle {
@Test
public void testLifecycle() {
-
lifecycle.fireStages();
}
@@ -76,8 +105,5 @@ public class TestLifecycle {
}
}
-
-
-
}
diff --git a/catalog/src/main/java/com/ning/billing/catalog/CaseBillingAlignment.java b/catalog/src/main/java/com/ning/billing/catalog/CaseBillingAlignment.java
index 57dd49c..559ab60 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/CaseBillingAlignment.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/CaseBillingAlignment.java
@@ -25,6 +25,7 @@ import com.ning.billing.catalog.api.ProductCategory;
public class CaseBillingAlignment extends CasePhase<BillingAlignment> {
+
@XmlElement(required=true)
private BillingAlignment alignment;
diff --git a/catalog/src/main/java/com/ning/billing/catalog/CatalogService.java b/catalog/src/main/java/com/ning/billing/catalog/CatalogService.java
index 6f04aa6..a7dde73 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/CatalogService.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/CatalogService.java
@@ -31,7 +31,7 @@ public class CatalogService implements IService, Provider<ICatalog>, ICatalogSer
private static ICatalog catalog;
private final ICatalogConfig config;
- private boolean isInitialized;
+ private final boolean isInitialized;
@Inject
@@ -41,6 +41,8 @@ public class CatalogService implements IService, Provider<ICatalog>, ICatalogSer
this.isInitialized = false;
}
+
+ /*
@Override
public synchronized void initialize() throws ServiceException {
if (!isInitialized) {
@@ -53,11 +55,6 @@ public class CatalogService implements IService, Provider<ICatalog>, ICatalogSer
}
}
- public String getName() {
- return CATALOG_SERVICE_NAME;
- }
-
-
@Override
public void start() throws ServiceException {
// Intentionally blank
@@ -69,6 +66,16 @@ public class CatalogService implements IService, Provider<ICatalog>, ICatalogSer
// Intentionally blank
}
+ */
+
+
+
+ @Override
+ public String getName() {
+ return CATALOG_SERVICE_NAME;
+ }
+
+
/* (non-Javadoc)
* @see com.ning.billing.catalog.ICatlogService#getCatalog()
diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java b/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
index 0db9538..597d3e5 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/engine/core/Engine.java
@@ -89,6 +89,7 @@ public class Engine implements IEventListener, IEntitlementService {
}
+ /*
@Override
public void initialize() {
this.catalog = catalogService.getCatalog();
@@ -106,6 +107,8 @@ public class Engine implements IEventListener, IEntitlementService {
public void stop() {
apiEventProcessor.stopNotifications();
}
+ */
+
@Override
public IEntitlementUserApi getUserApi(List<IApiListener> listeners) {
diff --git a/entitlement/src/test/java/com/ning/billing/entitlement/api/user/TestUserApiBase.java b/entitlement/src/test/java/com/ning/billing/entitlement/api/user/TestUserApiBase.java
index 13786fb..15b124c 100644
--- a/entitlement/src/test/java/com/ning/billing/entitlement/api/user/TestUserApiBase.java
+++ b/entitlement/src/test/java/com/ning/billing/entitlement/api/user/TestUserApiBase.java
@@ -119,15 +119,20 @@ public abstract class TestUserApiBase {
clock = (ClockMock) g.getInstance(IClock.class);
try {
+ /*
catalogService.initialize();
entitlementService.initialize();
+*/
init();
} catch (EntitlementUserApiException e) {
Assert.fail(e.getMessage());
+ }
+ /*
} catch (ServiceException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
+ */
}
protected abstract Injector getInjector();
@@ -149,6 +154,7 @@ public abstract class TestUserApiBase {
@BeforeMethod(groups={"setup"})
public void setupTest() {
+ /*
try {
log.warn("\n");
log.warn("RESET TEST FRAMEWORK\n\n");
@@ -162,20 +168,27 @@ public abstract class TestUserApiBase {
Assert.fail(e.getMessage());
}
assertNotNull(bundle);
- entitlementService.start();
+ // STEPH
+ //entitlementService.start();
} catch (ServiceException e) {
Assert.fail(e.getMessage());
}
+ */
}
@AfterMethod(groups={"setup"})
public void cleanupTest() {
+ /*
try {
- entitlementService.stop();
+ // STEPH
+ //entitlementService.stop();
log.warn("DONE WITH TEST\n");
+
} catch (ServiceException e) {
Assert.fail(e.getMessage());
+
}
+ */
}
// Glue magic to invoke the real test
pom.xml 5(+5 -0)
diff --git a/pom.xml b/pom.xml
index 8223f0a..b0b8952 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,6 +56,11 @@
</dependency>
<dependency>
<groupId>com.ning.billing</groupId>
+ <artifactId>killbill-entitlement</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.ning.billing</groupId>
<artifactId>killbill-catalog</artifactId>
<version>${project.version}</version>
</dependency>