package com.defonds.cloud.tool.config.util; import com.defonds.cloud.tool.config.encrypt.Encryptor; import com.defonds.cloud.tool.config.encrypt.factory.EncryptorFactory; import com.defonds.cloud.tool.config.encrypt.factory.EncryptorFactoryLinux; import com.defonds.cloud.tool.config.encrypt.factory.EncryptorFactoryMac; import com.defonds.cloud.tool.config.encrypt.factory.EncryptorFactoryWindows; public class EncryptorUtil { private static String osName = System.getProperties().getProperty("os.name"); private static Encryptor encryptor; static { EncryptorFactory encryptorFactory; if (osName.equals("Linux")) { // os is linux encryptorFactory = new EncryptorFactoryLinux(); } else if (osName.contains("Windows")) { // os is windows encryptorFactory = new EncryptorFactoryWindows(); } else { // os is mac encryptorFactory = new EncryptorFactoryMac(); } encryptor = encryptorFactory.getEncryptor(); } /** * * @param content - the string to be encrypt or decrypt * @param flag - encrypt flag, true is encrypt, false is decrypt * @return - string after encrypt or decrypt */ public static String encryptorStr(String content, boolean flag) { return EncryptorUtil.encryptor.encrypt(content, flag); } }
package com.defonds.cloud.tool.config.encrypt; public interface Encryptor { /** * @param content str to be encrypted * @param flag true:encrypt false:decrypt * @return */ public String encrypt(String content, boolean flag); }
package com.defonds.cloud.tool.config.encrypt; import java.io.IOException; import com.defonds.cloud.tool.config.util.NativeUtils; public class EncryptorLinuxAmd64 implements Encryptor { // Native method declaration // use the keyword "native" indicate this is an ‘unsafe‘ mtehod for java native String encryptStr(String content, boolean flag); // Load the library static { try { System.loadLibrary("libaesjni"); }catch (UnsatisfiedLinkError e) { try { NativeUtils.loadLibraryFromJar("/com/defonds/cloud/tool/config/encrypt/native/linux/amd64/libaesjni.so"); } catch (IOException e1) { throw new RuntimeException(e1); } // during runtime. .DLL within .JAR } } @Override public String encrypt(String content, boolean flag) { // TODO Auto-generated method stub return this.encryptStr(content, flag); } }
package com.defonds.cloud.tool.config.encrypt.factory; import com.defonds.cloud.tool.config.encrypt.Encryptor; public interface EncryptorFactory { public Encryptor getEncryptor(); }
package com.defonds.cloud.tool.config.encrypt.factory; import com.defonds.cloud.tool.config.encrypt.Encryptor; import com.defonds.cloud.tool.config.encrypt.EncryptorLinuxAmd64; import com.defonds.cloud.tool.config.encrypt.EncryptorLinuxI386; public class EncryptorFactoryLinux implements EncryptorFactory { private static String osArch = System.getProperties().getProperty("os.arch"); @Override public Encryptor getEncryptor() { if (osArch.equals("amd64")) { // os is linux amd64 return new EncryptorLinuxAmd64(); } else { // os is linux i386 return new EncryptorLinuxI386(); } } }
String abc = "abc"; System.out.println("before encrypt:" + abc); String abcEncrypted = EncryptorUtil.encryptorStr(abc, true); System.out.println("after encrypt:" + abcEncrypted); String abc2 = EncryptorUtil.encryptorStr(abcEncrypted, false); System.out.println("after decrypt:" + abc2);
public class EncryptorFactory { private static Properties sp = System.getProperties(); private static String osName = sp.getProperty("os.name"); private static String osArch = sp.getProperty("os.arch"); private static int index2 = osName.indexOf("indows"); private static boolean isWindows = false; static { if (index2 != -1) { isWindows = true; } } public static Encryptor getEncryptor() { if (isWindows) { // os is windows if (osArch.equals("amd64")) { // os is windows amd64 return new EncryptorWindowsAmd64(); } else { // os is windows x86 return new EncryptorWindowsX86(); } } else { // os is linux if (osArch.equals("amd64")) { // os is linux amd64 return new EncryptorLinuxAmd64(); } else { // os is linux i386 return new EncryptorLinuxI386(); } } } }
设计模式实战应用之五:工厂方法模式,布布扣,bubuko.com
原文地址:http://blog.csdn.net/defonds/article/details/38304717