Examples.java
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / client / Examples.java
index 70ca577..07e5e28 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed 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.
@@ -58,7 +58,7 @@ import aaf.v2_0.Users;
 import aaf.v2_0.Users.User;
 
 public class Examples {
-    public static <C> String print(RosettaEnv env, String nameOrContentType, boolean optional) throws APIException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
+    public static <C> String print(RosettaEnv env, String nameOrContentType, boolean optional) throws APIException,  NoSuchMethodException,  IllegalAccessException, InvocationTargetException {
         // Discover ClassName
         String className = null;
         String version = null;
@@ -85,12 +85,12 @@ public class Examples {
         } else {
             className = nameOrContentType;
         }
-        
+
         // No Void.class in aaf.v2_0 package causing errors when trying to use a newVoidv2_0
         // method similar to others in this class. This makes it work, but is it right?
         if ("Void".equals(className))
             return "";
-                
+
         if ("1.1".equals(version)) {
             version = "v1_0";
         } else if (version!=null) {
@@ -98,32 +98,46 @@ public class Examples {
         } else {
             version = "v2_0";
         }
-        
-        Class<?> cls;
-        try {
-            cls = Examples.class.getClassLoader().loadClass("aaf."+version+'.'+className);
-        } catch (ClassNotFoundException e) {
-            throw new APIException(e);
+
+        Class<?> cls=null;
+        int minorIdx = version.indexOf('_');
+        if(minorIdx<0) {
+            throw new APIException("Invalid Interface Version " + version);
+        }
+        int minor = Integer.parseInt(version.substring(minorIdx+1));
+        String vprefix=version.substring(0, minorIdx+1);
+        while(cls==null && minor>=0) {
+            try {
+                cls = Examples.class.getClassLoader().loadClass("aaf."+vprefix+minor+'.'+className);
+            } catch (ClassNotFoundException e) {
+                if(--minor<0) {
+                    throw new APIException("No Example for Version " + version + " found.");
+                }
+            }
+        }
+
+        if(cls==null) {
+            throw new APIException("ERROR: " + "aaf."+vprefix+"X not found.");
         }
-        
+
         Method meth;
         try {
-            meth = Examples.class.getDeclaredMethod("new"+cls.getSimpleName()+version,boolean.class);
+            meth = Examples.class.getDeclaredMethod("new"+cls.getSimpleName()+vprefix+minor,boolean.class);
         } catch (Exception e) {
             throw new APIException("ERROR: " + cls.getName() + " does not have an Example in Code.  Request from AAF Developers");
         }
-        
+
         RosettaDF<C> df = env.newDataFactory(cls);
         df.option(Data.PRETTY);
-        
+
         Object data = meth.invoke(null,optional);
-        
+
         @SuppressWarnings("unchecked")
         String rv = df.newData().load((C)data).out(type).asString();
-//        Object obj = df.newData().in(type).load(rv).asObject();
+
         return rv;
     }
-    
+
     /*
      *  Set Base Class Request (easier than coding over and over)
      */
@@ -132,10 +146,9 @@ public class Examples {
         req.setStart(Chrono.timeStamp(gc));
         gc.add(GregorianCalendar.MONTH, 6);
         req.setEnd(Chrono.timeStamp(gc));
-//        req.setForce("false");
-        
+
     }
-    
+
     @SuppressWarnings("unused")
     private static Request newRequestv2_0(boolean optional) {
         Request r = new Request();
@@ -154,7 +167,7 @@ public class Examples {
         if (optional)setOptional(rpr);
         return rpr;
     }
-    
+
     @SuppressWarnings("unused")
     private static Roles newRolesv2_0(boolean optional) {
         Role r;
@@ -192,8 +205,8 @@ public class Examples {
 
         return rs;
     }
-    
-    
+
+
     @SuppressWarnings("unused")
     private static PermRequest newPermRequestv2_0(boolean optional) {
         PermRequest pr = new PermRequest();
@@ -202,12 +215,12 @@ public class Examples {
         pr.setAction("myAction");
         if (optional) {
             pr.setDescription("Short and meaningful verbiage about the Permission");
-            
+
             setOptional(pr);
         }
         return pr;
     }
-    
+
     @SuppressWarnings("unused")
     private static Perm newPermv2_0(boolean optional) {
         Perm pr = new Perm();
@@ -232,7 +245,7 @@ public class Examples {
         pr.setAction("myAction");
         return pr;
     }
-    
+
     @SuppressWarnings("unused")
     private static Perms newPermsv2_0(boolean optional) {
         Perms perms = new Perms();
@@ -254,18 +267,20 @@ public class Examples {
         p.getRoles().add("org.osaaf.myns.myRole2");
 
         return perms;
-        
+
     }
-    
+
     @SuppressWarnings("unused")
     private static UserRoleRequest newUserRoleRequestv2_0(boolean optional) {
         UserRoleRequest urr = new UserRoleRequest();
         urr.setRole("org.osaaf.myns.myRole");
         urr.setUser("ab1234@people.osaaf.org");
-        if (optional) setOptional(urr);
+        if (optional) { 
+           setOptional(urr);
+        }
         return urr;
     }
-    
+
     @SuppressWarnings("unused")
     private static NsRequest newNsRequestv2_0(boolean optional) {
         NsRequest nr = new NsRequest();
@@ -273,7 +288,7 @@ public class Examples {
         nr.getResponsible().add("ab1234@people.osaaf.org");
         nr.getResponsible().add("cd5678@people.osaaf.org");
         nr.getAdmin().add("zy9876@people.osaaf.org");
-        nr.getAdmin().add("xw5432@people.osaaf.org");        
+        nr.getAdmin().add("xw5432@people.osaaf.org");
         if (optional) {
             nr.setDescription("This is my Namespace to set up");
             nr.setType("APP");
@@ -281,28 +296,29 @@ public class Examples {
         }
         return nr;
     }
-    
-    
+
+
     @SuppressWarnings("unused")
     private static Nss newNssv2_0(boolean optional) {
         Ns ns;
-        
+
         Nss nss = new Nss();
-        nss.getNs().add(ns = new Nss.Ns());
+        ns = new Nss.Ns();
+        nss.getNs().add(ns);
         ns.setName("org.osaaf.myns");
         ns.getResponsible().add("ab1234@people.osaaf.org");
         ns.getResponsible().add("cd5678@people.osaaf.org");
         ns.getAdmin().add("zy9876@people.osaaf.org");
         ns.getAdmin().add("xw5432@people.osaaf.org");
         ns.setDescription("This is my Namespace to set up");
-        
-        nss.getNs().add(ns = new Nss.Ns());
+        ns = new Nss.Ns();
+        nss.getNs().add(ns);
         ns.setName("org.osaaf.myOtherNs");
         ns.getResponsible().add("ab1234@people.osaaf.org");
         ns.getResponsible().add("cd5678@people.osaaf.org");
         ns.getAdmin().add("zy9876@people.osaaf.org");
-        ns.getAdmin().add("xw5432@people.osaaf.org");        
-            
+        ns.getAdmin().add("xw5432@people.osaaf.org");
+
         return nss;
     }
     @SuppressWarnings("unused")
@@ -329,23 +345,23 @@ public class Examples {
 
         return cr;
     }
-    
+
     @SuppressWarnings("unused")
     private static Users newUsersv2_0(boolean optional) {
         User user;
-    
+
         Users users = new Users();
     user = new Users.User();
         users.getUser().add(user);
-        user.setId("ab1234@people.osaaf.org");    
+        user.setId("ab1234@people.osaaf.org");
         GregorianCalendar gc = new GregorianCalendar();
         user.setExpires(Chrono.timeStamp(gc));
 
     user = new Users.User();
         users.getUser().add(user);
-        user.setId("zy9876@people.osaaf.org");    
-        user.setExpires(Chrono.timeStamp(gc));    
-            
+        user.setId("zy9876@people.osaaf.org");
+        user.setExpires(Chrono.timeStamp(gc));
+
         return users;
     }
 
@@ -386,7 +402,7 @@ public class Examples {
         ur.setRole("com.test.myapp.myRole");
         ur.setExpires(Chrono.timeStamp());
         urs.getUserRole().add(ur);
-        
+
         ur = new UserRole();
         ur.setUser("yx4321");
         ur.setRole("com.test.yourapp.yourRole");
@@ -439,7 +455,7 @@ public class Examples {
         return a;
     }
 
-    
+
 
     @SuppressWarnings("unused")
     private static aaf.v2_0.Error newErrorv2_0(boolean optional) {