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.
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
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:
- package org.myapp;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- /**
- * Simple class meant to read a properties file
- *
- * @author Jaikiran Pai
- *
- */
- public class MyPropertiesReader {
- /**
- * Default Constructor
- *
- */
- public MyPropertiesReader() {
- }
- /**
- * Some Method
- *
- * @throws IOException
- *
- */
- public void doSomeOperation() throws IOException {
- // Get the inputStream
- InputStream inputStream = this.getClass().getClassLoader()
- .getResourceAsStream("myApp.properties");
- Properties properties = new Properties();
- System.out.println("InputStream is: " + inputStream);
- // load the inputStream using the Properties
- properties.load(inputStream);
- // get the value of the property
- String propValue = properties.getProperty("abc");
- System.out.println("Property value is: " + propValue);
- }
- }
package org.myapp;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Simple class meant to read a properties file
*
* @author Jaikiran Pai
*
*/
public class MyPropertiesReader {
/**
* Default Constructor
*
*/
public MyPropertiesReader() {
}
/**
* Some Method
*
* @throws IOException
*
*/
public void doSomeOperation() throws IOException {
// Get the inputStream
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream("myApp.properties");
Properties properties = new Properties();
System.out.println("InputStream is: " + inputStream);
// load the inputStream using the Properties
properties.load(inputStream);
// get the value of the property
String propValue = properties.getProperty("abc");
System.out.println("Property value is: " + propValue);
}
}
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:
- public void doSomeOperation() throws IOException {
- //Get the inputStream-->This time we have specified the folder name too.
- InputStream inputStream = this.getClass().getClassLoader()
- .getResourceAsStream("config/myApp.properties");
- Properties properties = new Properties();
- System.out.println("InputStream is: " + inputStream);
- //load the inputStream using the Properties
- properties.load(inputStream);
- //get the value of the property
- String propValue = properties.getProperty("abc");
- System.out.println("Property value is: " + propValue);
- }
public void doSomeOperation() throws IOException {
//Get the inputStream-->This time we have specified the folder name too.
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream("config/myApp.properties");
Properties properties = new Properties();
System.out.println("InputStream is: " + inputStream);
//load the inputStream using the Properties
properties.load(inputStream);
//get the value of the property
String propValue = properties.getProperty("abc");
System.out.println("Property value is: " + propValue);
}
That's all that is required to get it working.
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
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
//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);
}
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 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);