Skip to content

Commit

Permalink
change the code iterating the loader #431
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Dec 4, 2023
1 parent 0515053 commit 44c1206
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 57 deletions.
41 changes: 23 additions & 18 deletions lphy-base/src/main/java/lphy/base/spi/SequenceTypeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,33 @@ private void registerExtensions(String extClsName) {

dataTypeMap = new ConcurrentHashMap<>();

try {
//*** SequenceTypeExtensionImpl must have a public no-args constructor ***//
for (SequenceTypeExtension sequenceTypeExtension : loader) {
// extClsName == null then register all
if (extClsName == null || sequenceTypeExtension.getClass().getName().equalsIgnoreCase(extClsName)) {
System.out.println("Registering extension from " + sequenceTypeExtension.getClass().getName());

// sequence types
Map<String, ? extends SequenceType> newDataTypes = sequenceTypeExtension.getSequenceTypes();
if (newDataTypes != null)
// TODO validate same sequence type?
newDataTypes.forEach(dataTypeMap::putIfAbsent);
}
//*** SequenceTypeExtensionImpl must have a public no-args constructor ***//
Iterator<SequenceTypeExtension> extensions = loader.iterator();

while (extensions.hasNext()) {
SequenceTypeExtension sequenceTypeExtension = null;
try {
//*** LPhyExtensionImpl must have a public no-args constructor ***//
sequenceTypeExtension = extensions.next();
} catch (ServiceConfigurationError serviceError) {
System.err.println(serviceError.getMessage());
serviceError.printStackTrace();
}

System.out.println("LPhy sequence types : " + Arrays.toString(dataTypeMap.values().toArray(new SequenceType[0])));
// for (SequenceTypeExtension sequenceTypeExtension : loader) {
// extClsName == null then register all
if (extClsName == null || sequenceTypeExtension.getClass().getName().equalsIgnoreCase(extClsName)) {
System.out.println("Registering extension from " + sequenceTypeExtension.getClass().getName());

} catch (ServiceConfigurationError serviceError) {
System.err.println(serviceError);
serviceError.printStackTrace();
}
// sequence types
Map<String, ? extends SequenceType> newDataTypes = sequenceTypeExtension.getSequenceTypes();
if (newDataTypes != null)
// TODO validate same sequence type?
newDataTypes.forEach(dataTypeMap::putIfAbsent);
}
} // end while

System.out.println("LPhy sequence types : " + Arrays.toString(dataTypeMap.values().toArray(new SequenceType[0])));
}

/**
Expand Down
82 changes: 43 additions & 39 deletions lphy/src/main/java/lphy/core/spi/ValueFormatterLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,62 +46,66 @@ private void registerExtensions(String extClsName) {

// simulatorListeners = new HashMap<>();

try {
for (LPhyValueFormatter valueFormatterSPI : loader) {
//*** SequenceTypeExtensionImpl must have a public no-args constructor ***//
Iterator<LPhyValueFormatter> extensions = loader.iterator();

if (extClsName == null || valueFormatterSPI.getClass().getName().equalsIgnoreCase(extClsName)) {
System.out.println("Registering extension from " + valueFormatterSPI.getClass().getName());
// ValueFormatter
Set<Class<? extends ValueFormatter>> formatterSet = valueFormatterSPI.getValueFormatters();
while (extensions.hasNext()) {
LPhyValueFormatter valueFormatterSPI = null;
try {
//*** LPhyExtensionImpl must have a public no-args constructor ***//
valueFormatterSPI = extensions.next();
} catch (ServiceConfigurationError serviceError) {
System.err.println(serviceError.getMessage());
serviceError.printStackTrace();
}

//TODO better code ?
// for (LPhyValueFormatter valueFormatterSPI : loader) {
// extClsName == null then register all
if (extClsName == null || valueFormatterSPI.getClass().getName().equalsIgnoreCase(extClsName)) {
System.out.println("Registering extension from " + valueFormatterSPI.getClass().getName());
// ValueFormatter
Set<Class<? extends ValueFormatter>> formatterSet = valueFormatterSPI.getValueFormatters();

//TODO better code ?
// for (Map.Entry<Class<?>, Set<Class<? extends ValueFormatter>>> entry : formatterMap.entrySet()) {
for (Class<? extends ValueFormatter> vFCls : formatterSet) {
// get the data type
for (Class<? extends ValueFormatter> vFCls : formatterSet) {
// get the data type
// Class<?> typeCls = vFCls.getTypeParameters()[0].getClass();
Class<?> typeCls = null;
try {
Method method = vFCls.getMethod("getDataTypeClass");
typeCls = GeneratorUtils.getGenericReturnType(method);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
if (typeCls == null)
LoggerUtils.log.severe("Extension " + valueFormatterSPI.getExtensionName() +
" : ValueFormatter '" + vFCls.getName() + "' cannot find data type !");

Set<Class<? extends ValueFormatter>> valFormSet = valueFormatterClasses
.computeIfAbsent(typeCls, k -> new HashSet<>());
Class<?> typeCls = null;
try {
Method method = vFCls.getMethod("getDataTypeClass");
typeCls = GeneratorUtils.getGenericReturnType(method);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
if (typeCls == null)
LoggerUtils.log.severe("Extension " + valueFormatterSPI.getExtensionName() +
" : ValueFormatter '" + vFCls.getName() + "' cannot find data type !");

Set<Class<? extends ValueFormatter>> valFormSet = valueFormatterClasses
.computeIfAbsent(typeCls, k -> new HashSet<>());
// Set<Class<? extends ValueFormatter>> newValFormSet = entry.getValue();

// warning, if the same ValueFormatter exists for the same Class in valueFormatterClasses map
// warning, if the same ValueFormatter exists for the same Class in valueFormatterClasses map
// newValFormSet.forEach(dataType -> {
if (valFormSet.contains(vFCls))
LoggerUtils.log.warning("Extension " + valueFormatterSPI.getExtensionName() +
" : ValueFormatter '" + vFCls.getName() + "' already exists in ValueFormatter map !");
valFormSet.add(vFCls);
if (valFormSet.contains(vFCls))
LoggerUtils.log.warning("Extension " + valueFormatterSPI.getExtensionName() +
" : ValueFormatter '" + vFCls.getName() + "' already exists in ValueFormatter map !");
valFormSet.add(vFCls);
// });

}
// SimulatorListener
}
// SimulatorListener
// List<Class<? extends SimulatorListener>> listeners = valueFormatterSPI.getSimulatorListenerClasses();
// LoaderManager.registerClasses(listeners, simulatorListeners);
}
}
} // end if
} // end while

System.out.println("Register value formatter for : " +
Arrays.toString(valueFormatterClasses.keySet().stream()
// sorted SimpleName of Classes
.map(Class::getSimpleName).sorted().toArray() ));

// System.out.println("LPhy simulator listener : " + Arrays.toString(simulatorListeners.keySet().toArray()));


} catch (ServiceConfigurationError serviceError) {
System.err.println(serviceError);
serviceError.printStackTrace();
}

}

public static Class<?> getReturnType(Class<?> genClass) {
Expand Down

0 comments on commit 44c1206

Please sign in to comment.