2012年5月2日星期三

15.5. optparse — Parser for command line options — Python v2.7.3 documentation

15.5. optparse — Parser for command line options — Python v2.7.3 documentation

Deprecated since version 2.7: The optparse module is deprecated and will not be developed further;
development will continue with the argparse module.

My Wiki: How to read a properties file in a web application

My Wiki: How to read a properties file in a web application

How to read a properties file in a web application

The code to do this is pretty simple. But going by the number of people who keep asking this question, i thought i would post the code over here. Let's consider that you have a war file named SampleApp.war which has a properties file named myApp.properties at it's root :

SampleApp.war
|
|-------- myApp.properties
|
|-------- WEB-INF
|
|---- classes
|
|----- org
|------ myApp
|------- MyPropertiesReader.class





Let's assume that you want to read the property named "abc" present in the properties file:

----------------
myApp.properties:
----------------

abc=some value
xyz=some other value



Let's consider that the class org.myApp.MyPropertiesReader present in your application wants to read the property. Here's the code for the same:


  1. package org.myapp;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. /** 
  8.  * Simple class meant to read a properties file 
  9.  *  
  10.  * @author Jaikiran Pai 
  11.  *  
  12.  */  
  13. public class MyPropertiesReader {  
  14.   
  15.     /** 
  16.      * Default Constructor 
  17.      *  
  18.      */  
  19.     public MyPropertiesReader() {  
  20.   
  21.     }  
  22.   
  23.     /** 
  24.      * Some Method 
  25.      *  
  26.      * @throws IOException 
  27.      *  
  28.      */  
  29.     public void doSomeOperation() throws IOException {  
  30.         // Get the inputStream  
  31.         InputStream inputStream = this.getClass().getClassLoader()  
  32.                 .getResourceAsStream("myApp.properties");  
  33.   
  34.         Properties properties = new Properties();  
  35.   
  36.         System.out.println("InputStream is: " + inputStream);  
  37.   
  38.         // load the inputStream using the Properties  
  39.         properties.load(inputStream);  
  40.         // get the value of the property  
  41.         String propValue = properties.getProperty("abc");  
  42.   
  43.         System.out.println("Property value is: " + propValue);  
  44.     }  
  45.   
  46. }  




Pretty straight-forward. Now suppose the properties file is not at the root of the application, but inside a folder (let's name it config) in the web application, something like:


SampleApp.war
|
|-------- config
| |------- myApp.properties
|
|
|-------- WEB-INF
|
|---- classes
|
|----- org
|------ myApp
|------- MyPropertiesReader.class




There will just be one line change in the above code:



  1. public void doSomeOperation() throws IOException {  
  2.       //Get the inputStream-->This time we have specified the folder name too.  
  3.       InputStream inputStream = this.getClass().getClassLoader()  
  4.               .getResourceAsStream("config/myApp.properties");  
  5.         
  6.       Properties properties = new Properties();  
  7.          
  8.       System.out.println("InputStream is: " + inputStream);  
  9.         
  10.       //load the inputStream using the Properties  
  11.       properties.load(inputStream);  
  12.       //get the value of the property  
  13.       String propValue = properties.getProperty("abc");  
  14.         
  15.       System.out.println("Property value is: " + propValue);  
  16.   }  
  17.     



That's all that is required to get it working.

i have apply a blogspot service as my english technology blog service

it seems most cnblogs audices comes from baidu,cnblogs currently didn't face to or optiminize for  the global developer so i open an english blog

scribefire also support sync artilce to multple blog just one more step,it's not difficut

http://maolingzhi.blogspots.com

i have apply a blogspot service as my english technology blog service

it seems most cnblogs audices comes from baidu,cnblogs currently didn't face to or optiminize for  the global developer so i open an english blog

scribefire also support sync artilce to multple blog just one more step,it's not difficut

set http proxy on httpclient4

//proxy
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("conf/system.properties");
       Properties properties = new Properties(); 
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            logger.debug("load system.properties failure...");
        } 
        String r=properties.getProperty("crawlNode.httpProxy", "none").toLowerCase().trim();
        if (!r.equals("none")){
            String[] arr=StringUtils.split(r, ":");
            int port=80;
            String url=r;
            if (arr.length==2)
            {
                port=Integer.parseInt(arr[1]);
                url=arr[0].trim();
            }
            HttpHost proxy = new HttpHost(url, port);
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            logger.debug("start using http proxy {}:{}",url,port);
           
        }

HttpClient: Detect Proxy Settings or Configuration Script from HttpClient (I/O and Streams forum at JavaRanch)

HttpClient: Detect Proxy Settings or Configuration Script from HttpClient (I/O and Streams forum at JavaRanch)

PAC is used by user agents to automatically select the right proxy based on the return values given by the javascript function.

In your case, the HttpClient won't be knowing how to interpret it I guess. You have to write the logic necessary to interpret the response and then connect using the right proxy.

But you state that even though you are hard coding the right proxy within your program, the connection is being refused. So check the parameters you are passing.

Are your proxy credentials correct, if any? Whats the HTTP status code you are getting?

Here are a few programs that work with PAC files. You can use them for testing your problem or analyze the code.

http://code.google.com/p/pacparser/
http://code.google.com/p/pactester/

Load a properties file - Real's Java How-to

Load a properties file - Real's Java How-to

Load a properties fileTag(s): Language

import java.util.Properties;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.net.URL;

public class PropsUtils {
private PropsUtils() { }
/**
* Load a properties file from the classpath
* @param propsName
* @return Properties
* @throws Exception
*/
public static Properties load(String propsName) throws Exception {
Properties props = new Properties();
URL url = ClassLoader.getSystemResource(propsName);
props.load(url.openStream());
return props;
}

/**
* Load a Properties File
* @param propsFile
* @return Properties
* @throws IOException
*/
public static Properties load(File propsFile) throws IOException {
Properties props = new Properties();
FileInputStream fis = new FileInputStream(propsFile);
props.load(fis);
fis.close();
return props;
}
}

Load from the startup directory of your application (ex. directory containing the jar)

java.util.Properties props = new java.util.Properties();
String path = getClass().getProtectionDomain().getCodeSource().
getLocation().toString().substring(6);
java.io.FileInputStream fis = new java.io.FileInputStream
(new java.io.File( path + "/myprops.props"));
props.load(fis);
fis.close();
System.out.println(props);