Skip to content

Commit

Permalink
refactor to make app keys an input to ClassSource_Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-confino committed Dec 5, 2024
1 parent 68ae73a commit e4520af
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.text.MessageFormat;
import java.util.ArrayList;
Expand All @@ -26,7 +24,6 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -35,17 +32,14 @@
import com.ibm.websphere.ras.TraceComponent;
import com.ibm.websphere.ras.annotation.Trivial;
import com.ibm.ws.annocache.service.internal.AnnotationCacheServiceImpl_Logging;
import com.ibm.ws.kernel.service.util.ServiceCaller;
import com.ibm.wsspi.anno.classsource.ClassSource_ScanCounts;
import com.ibm.wsspi.anno.classsource.ClassSource_ScanCounts.ResultField;
import com.ibm.wsspi.anno.service.AppKey;
import com.ibm.wsspi.anno.classsource.ClassSource_Streamer;
import com.ibm.wsspi.anno.service.AnnotationService_KeyService;
import com.ibm.wsspi.anno.service.AnnotationService_KeyService.AppKey;
import com.ibm.wsspi.annocache.classsource.ClassSource;
import com.ibm.wsspi.annocache.classsource.ClassSource_Aggregate;
import com.ibm.wsspi.annocache.classsource.ClassSource_ClassLoader;
import com.ibm.wsspi.annocache.classsource.ClassSource_Exception;
import com.ibm.wsspi.annocache.classsource.ClassSource_Factory;
import com.ibm.wsspi.annocache.classsource.ClassSource_Options;
import com.ibm.wsspi.annocache.util.Util_InternMap;

Expand All @@ -70,9 +64,6 @@ public class ClassSourceImpl_Aggregate implements ClassSource_Aggregate {
public static final String CLASS_NAME = ClassSourceImpl_Aggregate.class.getSimpleName();

// Logging ...

private static final ServiceCaller<AnnotationService_KeyService> keyServiceServiceCaller = new ServiceCaller<AnnotationService_KeyService>(ClassSourceImpl_Aggregate.class,
AnnotationService_KeyService.class);

protected final String hashText;

Expand All @@ -91,11 +82,11 @@ public String toString() {
//

// Top O' the world

public ClassSourceImpl_Aggregate(
ClassSourceImpl_Factory factory,
Util_InternMap internMap,
String applicationName, String moduleName, String moduleCategoryName,
String applicationName, AppKey appKey, String moduleName, String moduleCategoryName,
ClassSource_Options options) {

String methodName = "<init>";
Expand All @@ -109,18 +100,7 @@ public ClassSourceImpl_Aggregate(
this.applicationName = applicationName;
this.moduleName = moduleName;
this.moduleCategoryName = moduleCategoryName;

Properties props = System.getProperties();
String inUnitTest = props.getProperty("running.unit.test");

if (inUnitTest != null && inUnitTest.equals("true")) {
this.applicationKey = new SoftReference<AppKey>(new AppKey(applicationName));
} else if (applicationName != ClassSource_Factory.UNNAMED_APP) {
AppKey appKey = keyServiceServiceCaller.run((AnnotationService_KeyService ks) -> ks.getKeyForApp(applicationName)).get();
this.applicationKey = new WeakReference<AppKey>(appKey);
} else {
this.applicationKey = new WeakReference<AppKey>(null);
}
this.applicationKey = new WeakReference<AppKey>(appKey);

//

Expand Down Expand Up @@ -202,7 +182,7 @@ protected String internClassName(String className, boolean doForce) {
protected final String applicationName;
protected final String moduleName;
protected final String moduleCategoryName;
protected final Reference<AppKey> applicationKey;
protected final WeakReference<AppKey> applicationKey;

/**
* <p>Answer the name of the application of this class source.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
import com.ibm.wsspi.anno.classsource.ClassSource_MappedDirectory;
import com.ibm.wsspi.anno.classsource.ClassSource_MappedJar;
import com.ibm.wsspi.anno.classsource.ClassSource_MappedSimple.SimpleClassProvider;
import com.ibm.wsspi.anno.service.AppKey;
import com.ibm.wsspi.annocache.classsource.ClassSource_Aggregate;
import com.ibm.wsspi.annocache.classsource.ClassSource_Aggregate.ScanPolicy;
import com.ibm.wsspi.annocache.classsource.ClassSource_ClassLoader;
import com.ibm.wsspi.annocache.classsource.ClassSource_Exception;
import com.ibm.wsspi.annocache.classsource.ClassSource_Factory;
import com.ibm.wsspi.annocache.classsource.ClassSource_MappedSimple;
import com.ibm.wsspi.annocache.classsource.ClassSource_Options;
import com.ibm.wsspi.annocache.service.AnnotationCacheService_Service;
import com.ibm.wsspi.annocache.util.Util_Factory;
import com.ibm.wsspi.annocache.util.Util_InternMap;
import com.ibm.wsspi.annocache.util.Util_RelativePath;
Expand Down Expand Up @@ -166,13 +166,13 @@ public String getCanonicalName(String classSourceName) {
@Override
@Trivial
public ClassSourceImpl_Aggregate createAggregateClassSource(
String appName, String modName, String modCategoryName,
String appName, AppKey appKey, String modName, String modCategoryName,
ClassSource_Options options) throws ClassSource_Exception {

Util_InternMap classInternMap =
getUtilFactory().createInternMap(Util_InternMap.ValueType.VT_CLASS_NAME, "classes and packages");

return createAggregateClassSource(classInternMap, appName, modName, modCategoryName, options);
return createAggregateClassSource(classInternMap, appName, appKey, modName, modCategoryName, options);
// throws ClassSource_Exception
}

Expand Down Expand Up @@ -283,10 +283,10 @@ public ClassSourceImpl_ClassLoader createClassLoaderClassSource(
@Override
public ClassSourceImpl_Aggregate createAggregateClassSource(
Util_InternMap internMap,
String appName, String modName, String modCategoryName,
String appName, AppKey appKey, String modName, String modCategoryName,
ClassSource_Options options) throws ClassSource_Exception {

return new ClassSourceImpl_Aggregate(this, internMap, appName, modName, modCategoryName, options);
return new ClassSourceImpl_Aggregate(this, internMap, appName, appKey, modName, modCategoryName, options);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
import java.util.logging.Logger;

import com.ibm.websphere.ras.annotation.Trivial;

import com.ibm.wsspi.annocache.classsource.ClassSource_Exception;
import com.ibm.wsspi.annocache.classsource.ClassSource_Options;
import com.ibm.wsspi.annocache.classsource.ClassSource_Aggregate;
import com.ibm.wsspi.annocache.classsource.ClassSource_Aggregate.ScanPolicy;

import com.ibm.ws.annocache.classsource.internal.ClassSourceImpl_Aggregate;
import com.ibm.ws.annocache.classsource.internal.ClassSourceImpl_ClassLoader;
import com.ibm.ws.annocache.classsource.internal.ClassSourceImpl_Factory;
import com.ibm.ws.annocache.classsource.internal.ClassSourceImpl_MappedDirectory;
import com.ibm.ws.annocache.classsource.internal.ClassSourceImpl_MappedJar;
import com.ibm.ws.annocache.classsource.specification.ClassSource_Specification;
import com.ibm.ws.annocache.service.internal.AnnotationCacheServiceImpl_Logging;
import com.ibm.wsspi.anno.service.AppKey;
import com.ibm.wsspi.annocache.classsource.ClassSource_Aggregate;
import com.ibm.wsspi.annocache.classsource.ClassSource_Aggregate.ScanPolicy;
import com.ibm.wsspi.annocache.classsource.ClassSource_Exception;
import com.ibm.wsspi.annocache.classsource.ClassSource_Options;


public abstract class ClassSourceImpl_Specification implements ClassSource_Specification {
Expand Down Expand Up @@ -134,7 +133,7 @@ public ClassSourceImpl_Aggregate createEmptyRootClassSource(ClassSource_Options
throws ClassSource_Exception {

return getFactory().createAggregateClassSource(
getAppName(), getModName(), getModCategoryName(),
getAppName(), new AppKey(getAppName()), getModName(), getModCategoryName(),
classSourceOptions );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.WeakHashMap;

import com.ibm.websphere.ras.annotation.Trivial;
import com.ibm.wsspi.anno.service.AnnotationService_KeyService.AppKey;
import com.ibm.wsspi.anno.service.AppKey;
import com.ibm.wsspi.annocache.classsource.ClassSource_Factory;
import com.ibm.wsspi.annocache.targets.cache.TargetCache_ExternalConstants;
import com.ibm.wsspi.annocache.targets.cache.TargetCache_Options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package com.ibm.ws.annocache.targets.internal;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -39,7 +40,7 @@
import com.ibm.ws.annocache.util.internal.UtilImpl_NonInternSet;
import com.ibm.ws.annocache.util.internal.UtilImpl_Utils;
import com.ibm.wsspi.anno.classsource.ClassSource_Aggregate.ScanPolicy;
import com.ibm.wsspi.anno.service.AnnotationService_KeyService.AppKey;
import com.ibm.wsspi.anno.service.AppKey;
import com.ibm.wsspi.anno.util.Util_InternMap.ValueType;
import com.ibm.wsspi.annocache.classsource.ClassSource_Aggregate;
import com.ibm.wsspi.annocache.targets.AnnotationTargets_Exception;
Expand Down Expand Up @@ -463,7 +464,7 @@ protected void putSpecificResults(TargetsScannerBaseImpl scanner) {
protected ClassSource_Aggregate rootClassSource;

protected String appName;
protected AppKey appKey;
protected WeakReference<AppKey> appKey;
protected String modName;
protected String modCatName;
protected String modFullName;
Expand All @@ -476,7 +477,7 @@ protected void setRootClassSource(ClassSource_Aggregate rootClassSource) {

protected void setNames(ClassSource_Aggregate useRootClassSource) {
this.appName = useRootClassSource.getApplicationName();
this.appKey = useRootClassSource.getApplicationKey();
this.appKey = new WeakReference<AppKey>(useRootClassSource.getApplicationKey());
this.modName = useRootClassSource.getModuleName();
this.modCatName = useRootClassSource.getModuleCategoryName();

Expand All @@ -501,7 +502,7 @@ public String getAppName() {

@Trivial
public AppKey getAppKey() {
return appKey;
return appKey.get();
}

@Trivial
Expand Down

This file was deleted.

39 changes: 39 additions & 0 deletions dev/com.ibm.ws.anno/src/com/ibm/wsspi/anno/service/AppKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ibm.wsspi.anno.service;

public class AppKey {
private final String deploymentName;

public AppKey(String deploymentName) {
this.deploymentName = deploymentName;
}

public String getDeploymentName() {
return deploymentName;
}

@Override
public int hashCode() {
if (deploymentName == null) {
return 0;
}

return deploymentName.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AppKey other = (AppKey) obj;

if (deploymentName == null) {
return other.getDeploymentName() == null;
}

return deploymentName.equals(other.getDeploymentName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Set;
import java.util.logging.Logger;

import com.ibm.wsspi.anno.service.AnnotationService_KeyService.AppKey;
import com.ibm.wsspi.anno.service.AppKey;
import com.ibm.wsspi.annocache.util.Util_InternMap;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.ibm.ws.annocache.classsource.specification.ClassSource_Specification_Element;
import com.ibm.ws.annocache.classsource.specification.ClassSource_Specification_Elements;
import com.ibm.wsspi.adaptable.module.Container;
import com.ibm.wsspi.anno.service.AppKey;
import com.ibm.wsspi.annocache.util.Util_Factory;
import com.ibm.wsspi.annocache.util.Util_InternMap;
import com.ibm.wsspi.annocache.util.Util_RelativePath;
Expand Down Expand Up @@ -96,7 +97,14 @@ ClassSource_Exception wrapIntoClassSourceException(
* Create a new empty aggregate class source. Assign options to the new
* class source.
*
* appKey will only be stored in weak references, and the cache to annotation targets will
* be lost when it is deleted. It is the responsibility of the caller to store a reference
* to appKey and delete it when the application is uninstalled.
*
* appKey can be created with new AppKey("string that is unique to app");
*
* @param appName The name of the application of the class source.
* @param appKey A key for the cache that will store annotation targets.
* @param modName The name of the module of the class source.
* @param modNameCategory A name used to enable multiple results for
* the same module name.
Expand All @@ -107,7 +115,7 @@ ClassSource_Exception wrapIntoClassSourceException(
* @throws ClassSource_Exception Thrown if there was a problem creating the class source.
*/
ClassSource_Aggregate createAggregateClassSource(
String appName, String modName, String modNameCategory,
String appName, AppKey appKey, String modName, String modNameCategory,
ClassSource_Options options) throws ClassSource_Exception;

ClassSource_MappedSimple createSimpleClassSource(
Expand Down Expand Up @@ -162,7 +170,7 @@ ClassSource_ClassLoader createClassLoaderClassSource(

ClassSource_Aggregate createAggregateClassSource(
Util_InternMap internMap,
String appName, String modName, String modNameCategory,
String appName, AppKey appKey, String modName, String modNameCategory,
ClassSource_Options options) throws ClassSource_Exception;

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.ibm.wsspi.anno.classsource.ClassSource_Aggregate.ScanPolicy;
import com.ibm.wsspi.anno.classsource.ClassSource_ScanCounts;
import com.ibm.wsspi.anno.classsource.ClassSource_ScanCounts.ResultField;
import com.ibm.wsspi.anno.service.AppKey;
import com.ibm.wsspi.annocache.classsource.ClassSource_Exception;
import com.ibm.wsspi.annocache.classsource.ClassSource_Factory;
import com.ibm.wsspi.annocache.classsource.ClassSource_Streamer;
Expand All @@ -66,17 +67,18 @@ public class InfoStore_Internals_Test {

protected static ClassSourceImpl_Aggregate rootClassSource;
protected static InfoStoreImpl infoStore;

private static String APP_NAME = "TestEar";
//Store a reference so WeakReferences won't be garbage collected during a test
private static AppKey appKey = new AppKey(APP_NAME);

@BeforeClass
public static void setup() throws ClassSource_Exception, InfoStoreException {
UtilImpl_Factory utilImplFactory = new UtilImpl_Factory();
ClassSourceImpl_Factory factory = new ClassSourceImpl_Factory(utilImplFactory);

Properties props = System.getProperties();
props.setProperty("running.unit.test", "true");

ClassSourceImpl_Aggregate useRootClassSource =
factory.createAggregateClassSource("TestEar", "TestMod", ClassSource_Factory.UNSET_CATEGORY_NAME, factory.createOptions() );
factory.createAggregateClassSource(APP_NAME, appKey, "TestMod", ClassSource_Factory.UNSET_CATEGORY_NAME, factory.createOptions() );

ClassSourceImpl_MappedDirectory dirClassSource =
factory.createDirectoryClassSource(
Expand Down
Loading

0 comments on commit e4520af

Please sign in to comment.