package org.jivesoftware.smack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.compression.Java7ZlibInputOutputStream;
import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.provider.BindIQProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.sasl.core.SASLXOauth2Mechanism;
import org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism;
import org.jivesoftware.smack.util.FileUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public final class SmackInitialization {
   static final String SMACK_VERSION;
   private static final String DEFAULT_CONFIG_FILE = "classpath:org.jivesoftware.smack/smack-config.xml";
   private static final Logger LOGGER = Logger.getLogger(SmackInitialization.class.getName());

   public static void processConfigFile(InputStream cfgFileStream, Collection<Exception> exceptions) throws Exception {
      processConfigFile(cfgFileStream, exceptions, SmackInitialization.class.getClassLoader());
   }

   public static void processConfigFile(InputStream cfgFileStream, Collection<Exception> exceptions, ClassLoader classLoader) throws Exception {
      XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
      parser.setFeature("http://xmlpull.org/v1/doc/features.html#process-namespaces", true);
      parser.setInput(cfgFileStream, "UTF-8");
      int eventType = parser.getEventType();

      do {
         if (eventType == 2) {
            if (parser.getName().equals("startupClasses")) {
               parseClassesToLoad(parser, false, exceptions, classLoader);
            } else if (parser.getName().equals("optionalStartupClasses")) {
               parseClassesToLoad(parser, true, exceptions, classLoader);
            }
         }

         eventType = parser.next();
      } while(eventType != 1);

      try {
         cfgFileStream.close();
      } catch (IOException var6) {
         LOGGER.log(Level.SEVERE, "Error while closing config file input stream", var6);
      }

   }

   private static void parseClassesToLoad(XmlPullParser parser, boolean optional, Collection<Exception> exceptions, ClassLoader classLoader) throws XmlPullParserException, IOException, Exception {
      String startName = parser.getName();

      int eventType;
      String name;
      do {
         eventType = parser.next();
         name = parser.getName();
         if (eventType == 2 && "className".equals(name)) {
            String classToLoad = parser.nextText();
            if (!SmackConfiguration.isDisabledSmackClass(classToLoad)) {
               try {
                  loadSmackClass(classToLoad, optional, classLoader);
               } catch (Exception var9) {
                  if (exceptions == null) {
                     throw var9;
                  }

                  exceptions.add(var9);
               }
            }
         }
      } while(eventType != 3 || !startName.equals(name));

   }

   private static void loadSmackClass(String className, boolean optional, ClassLoader classLoader) throws Exception {
      Class initClass;
      try {
         initClass = Class.forName(className, true, classLoader);
      } catch (ClassNotFoundException var8) {
         Level logLevel;
         if (optional) {
            logLevel = Level.FINE;
         } else {
            logLevel = Level.WARNING;
         }

         LOGGER.log(logLevel, "A startup class '" + className + "' could not be loaded.");
         if (!optional) {
            throw var8;
         }

         return;
      }

      if (SmackInitializer.class.isAssignableFrom(initClass)) {
         SmackInitializer initializer = (SmackInitializer)initClass.newInstance();
         List<Exception> exceptions = initializer.initialize();
         if (exceptions != null && exceptions.size() != 0) {
            Iterator i$ = exceptions.iterator();

            while(i$.hasNext()) {
               Exception e = (Exception)i$.next();
               LOGGER.log(Level.SEVERE, "Exception in loadSmackClass", e);
            }
         } else {
            LOGGER.log(Level.FINE, "Loaded SmackInitializer " + className);
         }
      } else {
         LOGGER.log(Level.FINE, "Loaded " + className);
      }

   }

   static {
      String smackVersion;
      try {
         BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForUrl("classpath:org.jivesoftware.smack/version", (ClassLoader)null)));
         smackVersion = reader.readLine();

         try {
            reader.close();
         } catch (IOException var13) {
            LOGGER.log(Level.WARNING, "IOException closing stream", var13);
         }
      } catch (Exception var14) {
         LOGGER.log(Level.SEVERE, "Could not determine Smack version", var14);
         smackVersion = "unkown";
      }

      SMACK_VERSION = smackVersion;
      String disabledClasses = System.getProperty("smack.disabledClasses");
      if (disabledClasses != null) {
         String[] splitDisabledClasses = disabledClasses.split(",");
         String[] arr$ = splitDisabledClasses;
         int len$ = splitDisabledClasses.length;

         for(int i$ = 0; i$ < len$; ++i$) {
            String s = arr$[i$];
            SmackConfiguration.disabledSmackClasses.add(s);
         }
      }

      try {
         FileUtils.addLines("classpath:org.jivesoftware.smack/disabledClasses", SmackConfiguration.disabledSmackClasses);
      } catch (Exception var12) {
         throw new IllegalStateException(var12);
      }

      try {
         Class<?> c = Class.forName("org.jivesoftware.smack.CustomSmackConfiguration");
         Field f = c.getField("DISABLED_SMACK_CLASSES");
         String[] sa = (String[])((String[])f.get((Object)null));
         if (sa != null) {
            LOGGER.warning("Using CustomSmackConfig is deprecated and will be removed in a future release");
            String[] arr$ = sa;
            int len$ = sa.length;

            for(int i$ = 0; i$ < len$; ++i$) {
               String s = arr$[i$];
               SmackConfiguration.disabledSmackClasses.add(s);
            }
         }
      } catch (ClassNotFoundException var15) {
      } catch (NoSuchFieldException var16) {
      } catch (SecurityException var17) {
      } catch (IllegalArgumentException var18) {
      } catch (IllegalAccessException var19) {
      }

      InputStream configFileStream;
      try {
         configFileStream = FileUtils.getStreamForUrl("classpath:org.jivesoftware.smack/smack-config.xml", (ClassLoader)null);
      } catch (Exception var11) {
         throw new IllegalStateException(var11);
      }

      try {
         processConfigFile(configFileStream, (Collection)null);
      } catch (Exception var10) {
         throw new IllegalStateException(var10);
      }

      SmackConfiguration.compressionHandlers.add(new Java7ZlibInputOutputStream());

      try {
         if (Boolean.getBoolean("smack.debugEnabled")) {
            SmackConfiguration.DEBUG = true;
         }
      } catch (Exception var9) {
      }

      SASLAuthentication.registerSASLMechanism(new SCRAMSHA1Mechanism());
      SASLAuthentication.registerSASLMechanism(new SASLXOauth2Mechanism());
      ProviderManager.addIQProvider("bind", "urn:ietf:params:xml:ns:xmpp-bind", new BindIQProvider());
      SmackConfiguration.smackInitialized = true;
   }
}
