/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2018, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.wildfly.clustering.web.infinispan;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.ServiceLoader;

import org.wildfly.clustering.marshalling.spi.Serializer;
import org.wildfly.clustering.web.IdentifierSerializerProvider;
import org.wildfly.security.manager.WildFlySecurityManager;

/**
 * @author Paul Ferraro
 */
public enum SessionIdentifierSerializer implements Serializer<String> {
    INSTANCE;

    private final Serializer<String> serializer = WildFlySecurityManager.doUnchecked(new PrivilegedAction<Serializer<String>>() {
        @Override
        public Serializer<String> run() {
            Iterator<IdentifierSerializerProvider> providerIterator = ServiceLoader.load(IdentifierSerializerProvider.class, IdentifierSerializerProvider.class.getClassLoader()).iterator();
            if (!providerIterator.hasNext()) {
                throw new IllegalStateException();
            }
            return providerIterator.next().getSerializer();
        }
    });

    @Override
    public void write(DataOutput output, String value) throws IOException {
        this.serializer.write(output, value);
    }

    @Override
    public String read(DataInput input) throws IOException {
        return this.serializer.read(input);
    }
}

@@ -1212,16 +1212,60 @@
 erator;%0A
+import java.util.ServiceConfigurationError;%0A
 import j
@@ -1645,118 +1645,43 @@
 r = 
-WildFlySecurityManager.doUnchecked(new PrivilegedAction%3CSerializer%3CString%3E%3E() %7B%0A        @Override%0A        publ
+loadSerializer();%0A%0A    private stat
 ic S
@@ -1702,20 +1702,27 @@
 ng%3E 
-run
+loadSerializer
 () %7B%0A
-    
     
@@ -1776,33 +1776,12 @@
 ider
-Iterator = ServiceLoader.
+s = 
 load
@@ -1815,18 +1815,114 @@
 er.class
-, 
+).iterator();%0A        if (!providers.hasNext()) %7B%0A            throw new ServiceConfigurationError(
 Identifi
@@ -1955,32 +1955,14 @@
 .get
-ClassLoader()).iterator(
+Name()
 );%0A 
@@ -1971,98 +1971,217 @@
     
+ %7D%0A
      
-if (!providerIterator.hasNext()) %7B%0A                throw new IllegalStateException();
+   return providers.next().getSerializer();%0A    %7D%0A%0A    private static %3CT%3E Iterable%3CT%3E load(Class%3CT%3E providerClass) %7B%0A        PrivilegedAction%3CIterable%3CT%3E%3E action = new PrivilegedAction%3CIterable%3CT%3E%3E() %7B
 %0A   
@@ -2181,33 +2181,41 @@
 ) %7B%0A            
-%7D
+@Override
 %0A            ret
@@ -2215,56 +2215,129 @@
     
-return providerIterator.next().getSerializ
+public Iterable%3CT%3E run() %7B%0A                return ServiceLoader.load(providerClass, providerClass.getClassLoad
 er()
+)
 ;%0A
+    
     
@@ -2350,11 +2350,79 @@
     
-%7D);
+    %7D;%0A        return WildFlySecurityManager.doUnchecked(action);%0A    %7D
 %0A%0A  

Use privileged action for IdentifierSerializerProvider loading.