22
33import org .json .JSONException ;
44import org .reflections .Reflections ;
5+ import org .reflections .scanners .Scanners ;
56import org .slf4j .Logger ;
67import org .slf4j .LoggerFactory ;
78
@@ -22,11 +23,13 @@ public class DAO4JSONFactory {
2223
2324 /**
2425 * Does not allow {@link DAO4JSON} to be registered more than once.
25- * @param aClass one instance of the class.
26+ * @param dao one instance of the class.
2627 */
27- public static void registerDAO (DAO4JSON <?> dao ) {
28- if (!daoRegistry .containsKey (dao .getClassType ())) {
29- daoRegistry .put (dao .getClassType (),dao );
28+ public static void registerDAO (DataAccessObject dao ) {
29+ if (dao instanceof DAO4JSON <?> json ) {
30+ if (!daoRegistry .containsKey (json .getClassType ())) {
31+ daoRegistry .put (json .getClassType (), json );
32+ }
3033 }
3134 }
3235
@@ -35,7 +38,7 @@ public static void registerDAO(DAO4JSON<?> dao) {
3538 * @param aClass the class of the value
3639 * @param object the value
3740 * @return a value converted to JSON
38- * @throws JSONException
41+ * @throws JSONException if there is no DAO for the class
3942 */
4043 public static Object toJSON (Class <?> aClass ,Object object ) throws JSONException {
4144 DAO4JSON <?> dao = daoRegistry .get (aClass );
@@ -50,7 +53,7 @@ public static Object toJSON(Class<?> aClass,Object object) throws JSONException
5053 * @param aClass the class of the value
5154 * @param object the JSON
5255 * @return a value converted from JSON
53- * @throws JSONException
56+ * @throws JSONException if there is no DAO for the class
5457 */
5558 public static Object fromJSON (Class <?> aClass ,Object object ) throws JSONException {
5659 DAO4JSON <?> dao = daoRegistry .get (aClass );
@@ -90,13 +93,12 @@ public static String[] getNames() {
9093 }
9194
9295 public static void registerAllDAOInPackage (String packageName ) throws GraphException {
93- logger .info ("Registering all DAO in package: " + packageName );
9496 // Use Reflections to find all subtypes of Node in the package
95- Reflections reflections = new Reflections (packageName );
96- Set <Class <? extends DAO4JSON >> subTypes = reflections .getSubTypesOf (DAO4JSON .class );
97+ Reflections reflections = new Reflections (packageName , Scanners . SubTypes );
98+ Set <Class <? extends DataAccessObject >> subTypes = reflections .getSubTypesOf (DataAccessObject .class );
9799 var list = new ArrayList <>();
98100 subTypes .stream ().sorted (Comparator .comparing (Class ::getName )).forEach (typeFound ->{
99- // if(!typeFound.getName().startsWith(packageName)) return;
101+ if (!typeFound .getName ().startsWith (packageName )) return ;
100102 list .add (typeFound .getName ().substring (packageName .length () + 1 ));
101103 });
102104 String str = packageName + " contains DAO " + Arrays .toString (list .toArray ());
@@ -106,7 +108,7 @@ public static void registerAllDAOInPackage(String packageName) throws GraphExcep
106108 if (!typeFound .getName ().startsWith (packageName )) continue ;
107109
108110 try {
109- DAO4JSON <?> daoInstance = typeFound .getDeclaredConstructor ().newInstance ();
111+ DataAccessObject daoInstance = typeFound .getDeclaredConstructor ().newInstance ();
110112 registerDAO (daoInstance );
111113 } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
112114 throw new GraphException ("Failed to register DAO: " + typeFound .getName (), e );
@@ -117,4 +119,12 @@ public static void registerAllDAOInPackage(String packageName) throws GraphExcep
117119 static int size () {
118120 return daoRegistry .size ();
119121 }
122+
123+ /**
124+ * @param toFind the class to search for
125+ * @return true if the class is registered
126+ */
127+ public static boolean isRegistered (Class <?> toFind ) {
128+ return daoRegistry .containsKey (toFind );
129+ }
120130}
0 commit comments