package org.openntf.xpt.core.dss.encryption;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;

import com.ibm.designer.runtime.Application;
import com.ibm.xsp.application.ApplicationEx;

public class EncryptionService {
	private static final String ENC_SERVICE_KEY = "xpt.dss.encryption"; // $NON-NLS-1$

	private static final String ENC_DATAPROVIDER_SERVICE = "org.openntf.core.dss.encryption"; // $NON-NLS-1$

	private static final String PREF_PROVIDER = "xpt.dss.encryption.provider";
	
	private List<IEncryptionKeyProvider> m_ENCServices;
	
	private IEncryptionKeyProvider m_ENCService;

	public static EncryptionService getInstance() {
		EncryptionService cls = (EncryptionService) Application.get().getObject(ENC_SERVICE_KEY);
		if (cls == null) {
			synchronized (EncryptionService.class) {
				cls = (EncryptionService) Application.get().getObject(ENC_SERVICE_KEY);
				if (cls == null) {
					cls = new EncryptionService();
					Application.get().putObject(ENC_SERVICE_KEY, cls);

				}

			}
		}
		return cls;
	}
	
	
	public List<IEncryptionKeyProvider> getEncryptionProviders() {
		if (m_ENCServices == null) {
			AccessController.doPrivileged(new PrivilegedAction<Void>() {

				@SuppressWarnings("unchecked")
				@Override
				public Void run() {
					m_ENCServices = ApplicationEx.getInstance().findServices(ENC_DATAPROVIDER_SERVICE);

					return null;
				}
			});
		}
		return m_ENCServices;
	}
	
	
	public IEncryptionKeyProvider getEncryptionProvider() {
		if (m_ENCService == null) {
			AccessController.doPrivileged(new PrivilegedAction<Void>() {
				public Void run() {
					String providersProp = ApplicationEx.getInstance()
							.getApplicationProperty(PREF_PROVIDER, null);
					List<IEncryptionKeyProvider> allPDFProviders = getEncryptionProviders();
					for (IEncryptionKeyProvider p:  allPDFProviders) {
						if (p.getName().equalsIgnoreCase(providersProp)) {
							m_ENCService = p;
							break;
						}
					}
					if (m_ENCService == null) {
						//"No Provider found. Assign UNID Provider!");
						m_ENCService = new UNIDKeyProvider();
					}

					return null;
				}
			});
		}
		return m_ENCService;
	}
	
	public String encrypt(String strToEncrypt){
		if(strToEncrypt != null){
			return EncryptionFactory.encrypt(strToEncrypt, getEncryptionProvider().getKey());
		}
		return null;
	}

	public String decrypt(String strToDecrypt){
		if(strToDecrypt != null){
			return EncryptionFactory.decrypt(strToDecrypt, getEncryptionProvider().getKey());
		}
		return null;
	}
}

@@ -432,16 +432,20 @@
 openntf.
+xpt.
 core.dss
@@ -437,33 +437,33 @@
 tf.xpt.core.dss.
-e
+E
 ncryption%22; // $
@@ -1843,27 +1843,27 @@
 rovider%3E all
-PDF
+Enc
 Providers = 
@@ -1885,24 +1885,31 @@
 oviders();%0D%0A
+%09%09%09%09%09%0D%0A
 %09%09%09%09%09for (IE
@@ -1940,11 +1940,11 @@
  all
-PDF
+Enc
 Prov
@@ -2065,24 +2065,24 @@
 %09%7D%0D%0A%09%09%09%09%09%7D%0D%0A
-
 %09%09%09%09%09if (m_E
@@ -2099,24 +2099,120 @@
 == null) %7B%0D%0A
+%09%09%09%09%09%09if(allEncProviders.size() == 1)%7B%0D%0A%09%09%09%09%09%09%09m_ENCService = allEncProviders.get(0);%0D%0A%09%09%09%09%09%09%7D%0D%0A
 %09%09%09%09%09%09//%22No 

Change Name of EncryptionBinderService & take first