1 16 package org.apache.jetspeed.services.resources; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.FileInputStream ; 21 import java.util.Properties ; 22 import java.util.Iterator ; 23 import java.util.Map.Entry; 24 25 import javax.servlet.ServletConfig ; 26 27 import org.apache.turbine.services.resources.TurbineResourceService; 29 import org.apache.turbine.services.resources.ResourceService; 30 import org.apache.turbine.services.InitializationException; 31 32 import org.apache.commons.configuration.Configuration; 34 35 import org.apache.jetspeed.services.resources.JetspeedResources; 36 37 82 public class JetspeedResourceService 83 extends TurbineResourceService 84 { 85 92 public boolean getBoolean(String name) 93 { 94 95 return new Boolean (interpolate(getConfiguration().getString(name))).booleanValue(); 96 } 97 98 107 public boolean getBoolean(String name, boolean def) 108 { 109 110 String temp = interpolate(getConfiguration().getString(name)); 111 return temp != null ? new Boolean (temp).booleanValue() : def; 112 } 113 114 121 public double getDouble(String name) 122 { 123 124 return new Double (interpolate(getConfiguration().getString(name))).doubleValue(); 125 } 126 127 135 public double getDouble(String name, double def) 136 { 137 138 String temp = interpolate(getConfiguration().getString(name)); 139 return temp != null ? new Double (temp).doubleValue() : def; 140 } 141 142 149 public float getFloat(String name) 150 { 151 152 return new Float (interpolate(getConfiguration().getString(name))).floatValue(); 153 } 154 155 163 public float getFloat(String name, float def) 164 { 165 166 String temp = interpolate(getConfiguration().getString(name)); 167 return temp != null ? new Float (temp).floatValue() : def; 168 } 169 170 177 public int getInt(String name) 178 { 179 180 return new Integer (interpolate(getConfiguration().getString(name))).intValue(); 181 } 182 183 191 public int getInt(String name, int def) 192 { 193 194 String temp = interpolate(getConfiguration().getString(name)); 195 return temp != null ? new Integer (temp).intValue() : def; 196 } 197 198 205 public long getLong(String name) 206 { 207 208 return new Long (interpolate(getConfiguration().getString(name))).longValue(); 209 } 210 211 219 public long getLong(String name, long def) 220 { 221 222 String temp = interpolate(getConfiguration().getString(name)); 223 return temp != null ? new Long (temp).longValue() : def; 224 } 225 226 234 public ResourceService getResources(String prefix) 235 { 236 Configuration config = getConfiguration().subset(prefix); 237 238 if (config == null) 239 { 240 return null; 241 } 242 243 JetspeedResourceService res = new JetspeedResourceService(); 244 try 245 { 246 res.init(config); 247 } 248 catch (Exception e) 249 { 250 System.err.println("Exception in init of JetspeedResourceService" + e.getMessage()); 251 e.printStackTrace(); 252 } 253 254 return (ResourceService) res; 255 } 256 257 276 public void init() 277 throws InitializationException 278 { 279 System.out.println("Jetspeed Services: Starting with no parameters"); 280 super.init(); 281 } 282 283 public synchronized void init(ServletConfig config) throws InitializationException 284 { 285 String propsDir = null; 286 String appName = config.getServletName(); 287 String deployFilename = appName + ".properties"; 288 String torqueFilename = appName + "_torque.properties"; 289 super.init(config); 290 291 String version = getString(JetspeedResources.JETSPEED_VERSION_KEY); 294 String name = getString(JetspeedResources.JETSPEED_NAME_KEY); 295 if (version != null && name != null) 296 { 297 System.out.println(""); 298 System.out.println("Starting " + name + "/" + version); 299 System.out.println(""); 300 } 301 302 try 303 { 304 propsDir = System.getProperty("jetspeed.conf.dir", null); 305 if (null == propsDir) 306 { 307 return; 309 } 310 311 312 String torqueProps = makeFileNamePath(propsDir, torqueFilename); 313 String deployProps = makeFileNamePath(propsDir, deployFilename); 314 315 System.out.println("torque props = " + torqueProps); 316 System.out.println("deploy props = " + deployProps); 317 318 File deployFile = new File (deployProps); 319 if (deployFile.exists()) 320 { 321 FileInputStream is = new FileInputStream (deployProps); 322 Properties props = new Properties (); 323 props.load(is); 324 325 Iterator it = props.entrySet().iterator(); 326 while (it.hasNext()) 327 { 328 Entry entry = (Entry)it.next(); 329 this.setProperty((String )entry.getKey(), (String )entry.getValue()); 331 System.out.println("setting key/value: " + entry.getKey() + ":" + entry.getValue()); 332 } 333 } 334 else 335 { 336 String msg = "Failed to find Deploy properties: " + deployProps; 337 System.err.println(msg); 338 } 339 340 File torqueFile = new File (torqueProps); 341 if (torqueFile.exists()) 342 { 343 this.setProperty("component.torque.config", torqueProps); 344 345 FileInputStream tis = new FileInputStream (torqueProps); 346 Properties tprops = new Properties (); 347 tprops.load(tis); 348 349 System.out.println("Connecting to: "+tprops.getProperty("database.default.url")); 350 System.out.println("Database Username: "+tprops.getProperty("database.default.username")); 351 } 352 } 353 catch (IOException e) 354 { 355 StringBuffer msg = new StringBuffer ("Error reading properties for appName: "); 356 msg.append(appName); 357 msg.append(", props Dir: " + propsDir); 358 System.err.println("Exception in loading properties: " + propsDir); 359 e.printStackTrace(); 360 } 361 } 362 363 364 protected String makeFileNamePath(String propsDir, String fileName) 365 { 366 StringBuffer name = new StringBuffer (propsDir); 367 368 if (!propsDir.endsWith(File.separator)) 369 { 370 name.append(File.separator); 371 } 372 name.append(fileName); 373 return name.toString(); 374 } 375 376 } 377 | Popular Tags |