use sxc with hand-tweaked JAXB classes

git-svn-id: https://svn.apache.org/repos/asf/geronimo/components/jaspi/trunk@677037 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/geronimo-jaspi/pom.xml b/geronimo-jaspi/pom.xml
index 43dc99b..0d87253 100644
--- a/geronimo-jaspi/pom.xml
+++ b/geronimo-jaspi/pom.xml
@@ -165,6 +165,7 @@
             <groupId>com.envoisolutions.sxc</groupId>
             <artifactId>sxc-jaxb</artifactId>
         </dependency>
+
         <dependency>
             <groupId>org.codehaus.woodstox</groupId>
             <artifactId>wstx-asl</artifactId>
@@ -197,8 +198,6 @@
             <scope>test</scope>
         </dependency>
 
-
-
     </dependencies>
     <build>
         <pluginManagement>
@@ -243,25 +242,25 @@
         </pluginManagement>
 
         <plugins>
-            <!--
-                     <plugin>
-                      <groupId>com.envoisolutions.sxc</groupId>
-                      <artifactId>sxc-jaxb-maven-plugin</artifactId>
-                      <version>0.8-SNAPSHOT</version>
-                      <executions>
-                        <execution>
-                          <configuration>
-                            <classes>
-                              <class>org.apache.geronimo.components.jaspi</class>
-                            </classes>
-                          </configuration>
-                          <goals>
-                            <goal>generate</goal>
-                          </goals>
-                      </execution>
-                    </executions>
-                  </plugin>
-            -->
+
+            <!--<plugin>-->
+                <!--<groupId>com.envoisolutions.sxc</groupId>-->
+                <!--<artifactId>sxc-jaxb-maven-plugin</artifactId>-->
+                <!--<version>0.8-SNAPSHOT</version>-->
+                <!--<executions>-->
+                    <!--<execution>-->
+                        <!--<configuration>-->
+                            <!--<classes>-->
+                                <!--<class>org.apache.geronimo.components.jaspi.model</class>-->
+                            <!--</classes>-->
+                        <!--</configuration>-->
+                        <!--<goals>-->
+                            <!--<goal>generate</goal>-->
+                        <!--</goals>-->
+                    <!--</execution>-->
+                <!--</executions>-->
+            <!--</plugin>-->
+
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-enforcer-plugin</artifactId>
diff --git a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ConfigProviderMapAdapter.java b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ConfigProviderMapAdapter.java
index 4ec9f31..dd5e390 100644
--- a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ConfigProviderMapAdapter.java
+++ b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ConfigProviderMapAdapter.java
@@ -20,20 +20,20 @@
 
 package org.apache.geronimo.components.jaspi.model;
 
-import java.util.Map;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
-import java.util.ArrayList;
+import java.util.Map;
 
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 
 import org.apache.geronimo.components.jaspi.ClassLoaderLookup;
 
 /**
- * @version $Rev:$ $Date:$
+ * @version $Rev$ $Date$
  */
 public class ConfigProviderMapAdapter extends XmlAdapter<ConfigProviderType[], Map<String, ConfigProviderType>> {
-
+    public static ClassLoaderLookup staticClassLoaderLookup;
     private final ClassLoaderLookup classLoaderLookup;
 
     public ConfigProviderMapAdapter(ClassLoaderLookup classLoaderLookup) {
@@ -41,33 +41,41 @@
     }
 
     public ConfigProviderMapAdapter() {
-        ClassLoader testLoader = Thread.currentThread().getContextClassLoader();
-        final ClassLoader cl = testLoader == null? ConfigProviderMapAdapter.class.getClassLoader(): testLoader;
-        classLoaderLookup = new ClassLoaderLookup() {
+        if (staticClassLoaderLookup != null) {
+            this.classLoaderLookup = staticClassLoaderLookup;
+        } else {
+            ClassLoader testLoader = Thread.currentThread().getContextClassLoader();
+            final ClassLoader cl = testLoader == null ? ConfigProviderMapAdapter.class.getClassLoader() : testLoader;
+            classLoaderLookup = new ClassLoaderLookup() {
 
-            public ClassLoader getClassLoader(String name) {
-                return cl;
-            }
-        };
+                public ClassLoader getClassLoader(String name) {
+                    return cl;
+                }
+            };
+        }
     }
 
     public Map<String, ConfigProviderType> unmarshal(ConfigProviderType[] configProviderTypes) throws Exception {
         Map<String, ConfigProviderType> map = new HashMap<String, ConfigProviderType>();
-        for (ConfigProviderType configProviderType: configProviderTypes) {
-            String key = configProviderType.getRegistrationKey();
-            map.put(key, configProviderType);
-            configProviderType.createAuthConfigProvider(classLoaderLookup);
+        if (configProviderTypes != null) {
+            for (ConfigProviderType configProviderType : configProviderTypes) {
+                if (configProviderType != null) {
+                    String key = configProviderType.getRegistrationKey();
+                    map.put(key, configProviderType);
+                    configProviderType.createAuthConfigProvider(classLoaderLookup);
+                }
+            }
         }
         return map;
     }
 
     public ConfigProviderType[] marshal(Map<String, ConfigProviderType> stringConfigProviderTypeMap) throws Exception {
         List<ConfigProviderType> list = new ArrayList<ConfigProviderType>();
-        for (ConfigProviderType configProviderType: stringConfigProviderTypeMap.values()) {
+        for (ConfigProviderType configProviderType : stringConfigProviderTypeMap.values()) {
             if (configProviderType.isPersistent()) {
                 list.add(configProviderType);
             }
         }
-        return list.toArray(new ConfigProviderType[stringConfigProviderTypeMap.size()]);
+        return list.toArray(new ConfigProviderType[list.size()]);
     }
 }
diff --git a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/JaspiXmlUtil.java b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/JaspiXmlUtil.java
index 436633a..04f294a 100644
--- a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/JaspiXmlUtil.java
+++ b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/JaspiXmlUtil.java
@@ -23,6 +23,7 @@
 import java.io.Writer;
 import java.io.Reader;
 import java.io.IOException;
+import java.util.Collections;
 
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamException;
@@ -47,7 +48,9 @@
 
     static {
         try {
-            JASPI_CONTEXT = JAXBContext.newInstance(JaspiType.class);
+//            JASPI_CONTEXT = JAXBContext.newInstance(JaspiType.class);
+            JASPI_CONTEXT = com.envoisolutions.sxc.jaxb.JAXBContextImpl.newInstance(new Class[] {JaspiType.class}, Collections.singletonMap("com.envoisolutions.sxc.generate", "false"));
+
         } catch (JAXBException e) {
             throw new RuntimeException("Could not create jaxb contexts for plugin types", e);
         }
diff --git a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/StringMapAdapter.java b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/StringMapAdapter.java
index b0411ab..ff0de47 100644
--- a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/StringMapAdapter.java
+++ b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/StringMapAdapter.java
@@ -29,10 +29,13 @@
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 
 /**
- * @version $Rev:$ $Date:$
+ * @version $Rev$ $Date$
  */
 public class StringMapAdapter extends XmlAdapter<String, Map<String, String>> {
     public Map<String, String> unmarshal(String s) throws Exception {
+        if (s == null) {
+            return null;
+        }
         Properties properties = new Properties();
         ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
         properties.load(in);
@@ -44,6 +47,9 @@
     }
 
     public String marshal(Map<String, String> map) throws Exception {
+        if (map == null) {
+            return "";
+        }
         Properties properties = new Properties();
         properties.putAll(map);
         ByteArrayOutputStream out = new ByteArrayOutputStream();
diff --git a/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/model/JaxbTest.java b/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/model/JaxbTest.java
index 25e5f02..a999612 100644
--- a/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/model/JaxbTest.java
+++ b/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/model/JaxbTest.java
@@ -24,6 +24,11 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.File;
+import java.io.Writer;
+import java.io.FileWriter;
+import java.io.FileReader;
+import java.net.URL;
 
 import javax.xml.bind.JAXBException;
 import javax.xml.parsers.ParserConfigurationException;
@@ -43,11 +48,17 @@
     @Test
     public void testLoad() throws Exception {
         String file = "test-jaspi.xml";
-        JaspiType rbac = loadRbac(file);
-        
+        JaspiType jaspi1 = loadJaspi(file);
+        if (jaspi1.getConfigProvider().size() != 1) throw new Exception("expected 1 configprovider, not this: " + jaspi1.getConfigProvider());
+        URL url = getClass().getClassLoader().getResource("test-jaspi.xml");
+        File newFile = new File(new File(url.getPath()).getParentFile(), "test-jaspi-2.xml");
+        Writer writer = new FileWriter(newFile);
+        JaspiXmlUtil.writeJaspi(jaspi1, writer);
+        JaspiType jaspi2 = JaspiXmlUtil.loadJaspi(new FileReader(newFile));
+        if (jaspi2.getConfigProvider().size() != 1) throw new Exception("expected 1 configprovider, not this: " + jaspi2.getConfigProvider());
     }
 
-    private JaspiType loadRbac(String file) throws ParserConfigurationException, IOException, SAXException, JAXBException, XMLStreamException {
+    private JaspiType loadJaspi(String file) throws ParserConfigurationException, IOException, SAXException, JAXBException, XMLStreamException {
         InputStream in = getClass().getClassLoader().getResourceAsStream(file);
         Reader reader = new InputStreamReader(in);
         JaspiType rbac = JaspiXmlUtil.loadJaspi(reader);
diff --git a/geronimo-jaspi/src/test/resources/test-jaspi.xml b/geronimo-jaspi/src/test/resources/test-jaspi.xml
index f22b07c..0d1a060 100644
--- a/geronimo-jaspi/src/test/resources/test-jaspi.xml
+++ b/geronimo-jaspi/src/test/resources/test-jaspi.xml
@@ -24,7 +24,7 @@
         <description>description</description>
         <className>org.apache.geronimo.components.jaspi.providers.DummyProvider</className>
         <properties>foo=bar</properties>
-        <persistent>false</persistent>
+        <persistent>true</persistent>
     </configProvider>
 
 </jaspi>
\ No newline at end of file