1 52 package org.bsf.remoting.util.naming; 53 54 import javax.naming.Context ; 55 import javax.naming.InitialContext ; 56 import java.io.InputStream ; 57 import java.io.IOException ; 58 import java.util.Properties ; 59 60 74 public class PropertiesICFactory implements InitialContextFactory { 75 76 private String _propertiesRessources; 77 private Context _ic; 78 79 83 public void setPropertiesRessources(String propertiesRessources) { 84 boolean bTmpCheckResult = false; 85 86 if ( ( propertiesRessources != null ) && ( 87 !propertiesRessources.equals( "") ) ) 88 bTmpCheckResult = true; 89 90 if ( !bTmpCheckResult) 91 throw new IllegalArgumentException (); 92 _propertiesRessources = propertiesRessources; 93 loadICInstance(); 94 } 95 96 99 private void loadICInstance() { 100 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 101 InputStream inputStream = classLoader.getResourceAsStream(_propertiesRessources); 102 103 if ( inputStream == null) 104 throw new RuntimeException ("Unable to load " + _propertiesRessources); 105 106 Properties properties = new Properties (); 107 108 try { 109 properties.load(inputStream); 110 _ic = new InitialContext (properties); 111 } catch (Exception e) { 112 throw new RuntimeException (e.toString()); 113 } 114 } 115 116 119 public Context getInitialContext() { 120 if (_ic == null ) 121 throw new RuntimeException ("The properties name haven't been set."); 122 return _ic; 123 } 124 125 128 public static Context createInitialContext(String propertiesRessource) { 129 Context result = null; 130 131 Properties properties = new Properties (); 132 133 if ( propertiesRessource != null && propertiesRessource.length() > 0){ 136 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 137 InputStream inputStream = classLoader.getResourceAsStream(propertiesRessource); 138 139 if ( inputStream == null) 140 throw new RuntimeException ("Unable to load " + propertiesRessource); 141 142 143 try { 144 properties.load(inputStream); 145 } catch (IOException e) { 146 throw new RuntimeException (e.getLocalizedMessage()); 147 } 148 } 149 150 try { 151 result = new InitialContext (properties); 152 } catch (Exception e) { 153 throw new RuntimeException (e.getLocalizedMessage()); 154 } 155 return result; 156 } 157 158 } | Popular Tags |