/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Boris V. Kuznetsov
 * @version $Revision$
 */

package javax.net.ssl;

import java.io.FileInputStream;
import java.security.AccessController;
import java.security.Provider;
import java.security.Security;
import java.security.KeyStore;
import java.util.Iterator;

import org.apache.harmony.security.fortress.Engine;
import org.apache.harmony.security.fortress.Services;


/**
 * Support class for this package.
 *  
 */

class DefaultSSLContext {
    private static SSLContext defaultSSLContext;

    public static SSLContext getContext() {
        if (defaultSSLContext == null) {
            defaultSSLContext = AccessController
                    .doPrivileged(new java.security.PrivilegedAction<SSLContext>() {
                        public SSLContext run() {
                            return findDefault();
                        }
                    });
        }
        return defaultSSLContext;
    }

    private static SSLContext findDefault() {
        // FIXME EXPORT CONTROL
        Provider.Service service;
        for (Iterator it1 = Services.getProvidersList().iterator(); it1
                .hasNext();) {
            service = Engine.door.getService((Provider) it1.next(),
                    "SSLContext");
            if (service != null) {
                try {
                    SSLContext con = new ContextImpl(
                            (SSLContextSpi) service.newInstance(null),
                            service.getProvider(), 
                            service.getAlgorithm());

 //TODO javax.net.ssl.keyStoreProvider, javax.net.ssl.trustStoreProvider system property
                    // find KeyStore, KeyManagers
                    KeyManager[] keyManagers = null;
                    KeyStore ks = KeyStore.getInstance(KeyStore
                            .getDefaultType());
                    String keystore = System
                            .getProperty("javax.net.ssl.keyStore");
                    String keystorepwd = System
                            .getProperty("javax.net.ssl.keyStorePassword");
                    char[] pwd = null;
                    if (keystorepwd != null) {
                        pwd = keystorepwd.toCharArray();
                    }
                    if (keystore != null) {
                        FileInputStream fis = new java.io.FileInputStream(
                                keystore);
                        ks.load(fis, pwd);
                        fis.close();

                        KeyManagerFactory kmf;
                        String kmfAlg = Security
                                .getProperty("ssl.KeyManagerFactory.algorithm");
                        if (kmfAlg == null) {
                            kmfAlg = "SunX509";
                        }
                        kmf = KeyManagerFactory.getInstance(kmfAlg);
                        kmf.init(ks, pwd);
                        keyManagers = kmf.getKeyManagers();
                    }

                    // find TrustStore, TrustManagers
                    TrustManager[] trustManagers = null;
                    keystore = System.getProperty("javax.net.ssl.trustStore");
                    keystorepwd = System
                            .getProperty("javax.net.ssl.trustStorePassword");
                    pwd = null;
                    if (keystorepwd != null) {
                        pwd = keystorepwd.toCharArray();
                    }
                    //TODO Defaults: jssecacerts; cacerts
                    if (keystore != null) {
                        FileInputStream fis = new java.io.FileInputStream(
                                keystore);
                        ks.load(fis, pwd);
                        fis.close();
                        TrustManagerFactory tmf;
                        String tmfAlg = Security
                                .getProperty("ssl.TrustManagerFactory.algorithm");
                        if (tmfAlg == null) {
                            tmfAlg = "PKIX";
                        }
                        tmf = TrustManagerFactory.getInstance(tmfAlg);
                        tmf.init(ks);
                        trustManagers = tmf.getTrustManagers();
                    }

                    con.init(keyManagers, trustManagers, null);
                    return con;
                } catch (Exception e) {
                    // e.printStackTrace();
                    // ignore and try another
                }
            }
        }
        return null;
    }
}

@@ -985,24 +985,24 @@
 ecurity.
-Provider
+KeyStore
 ;%0Aimport
@@ -1016,24 +1016,32 @@
 ecurity.
-Security
+PrivilegedAction
 ;%0Aimport
@@ -1051,32 +1051,32 @@
 va.security.
-KeyStore
+Provider
 ;%0Aimport jav
@@ -1081,21 +1081,25 @@
 ava.
-util.Iterator
+security.Security
 ;%0A%0Ai
@@ -1204,17 +1204,16 @@
 vices;%0A%0A
-%0A
 /**%0A * S
@@ -1249,16 +1249,16 @@
 .%0A *
-  %0A */%0A%0A
+/%0Afinal 
 clas
@@ -1337,22 +1337,16 @@
     
-public
  static 
 SSLC
@@ -1333,32 +1333,45 @@
 t;%0A%0A     static 
+synchronized 
 SSLContext getCo
@@ -1508,30 +1508,16 @@
 ged(new 
-java.security.
 Privileg
@@ -1828,24 +1828,29 @@
         
+for (
 Provider
 .Service
@@ -1845,140 +1845,85 @@
 ider
-.Service service;%0A        for (Iterator it1 = Services.getProvidersList().iterator(); it1%0A                .hasNext();) %7B%0A           
+ provider : Services.getProvidersList()) %7B%0A            final Provider.Service
  ser
@@ -1956,50 +1956,17 @@
 ice(
-(P
+p
 rovider
-) it1.next(),%0A                   
+,
  %22SS
@@ -2091,37 +2091,8 @@
 mpl(
-%0A                            
 (SSL
@@ -2185,70 +2185,109 @@
 (), 
-%0A                            service.getAlgorithm());%0A%0A //TODO
+service.getAlgorithm());%0A%0A                    /* %0A                     * TODO %0A                     *
  jav
@@ -2315,16 +2315,40 @@
 ovider, 
+%0A                     * 
 javax.ne
@@ -2388,16 +2388,61 @@
 roperty%0A
+                     */%0A                    %0A
         
@@ -2603,37 +2603,8 @@
 tore
-%0A                            
 .get
@@ -2667,37 +2667,8 @@
 stem
-%0A                            
 .get
@@ -2754,37 +2754,8 @@
 stem
-%0A                            
 .get
@@ -3053,32 +3053,24 @@
 m fis = new 
-java.io.
 FileInputStr
@@ -3077,41 +3077,8 @@
 eam(
-%0A                                
 keys
@@ -3264,41 +3264,8 @@
 rity
-%0A                                
 .get
@@ -3858,37 +3858,8 @@
 stem
-%0A                            
 .get
@@ -4084,16 +4084,17 @@
       //
+ 
 TODO Def
@@ -4215,24 +4215,16 @@
 s = new 
-java.io.
 FileInpu
@@ -4235,41 +4235,8 @@
 eam(
-%0A                                
 keys
@@ -4423,41 +4423,8 @@
 rity
-%0A                                
 .get
@@ -4923,52 +4923,8 @@
 ) %7B%0A
-                    // e.printStackTrace();%0A
     

add synchronized keyword to keep thread-safe; general clean up of code