I am trying access a properties file in a library. The problem is that the file is located in the META-INF folder of the application which is using my library. I am currently using:
private static final String CONFIG_FILE = "config.properties";
private Properties getConfigFile(){
InputStream input = null;
Properties config = new Properties();
input = this.getClass().getResourceAsStream(CONFIG_FILE);
if(input == null){
System.out.println("*****************************************INPUT NULL DEST**************************************");
return null;
}
try {
config.load(input);
return config;
} catch (IOException io) {
if(input!=null){
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return config;
}`
But withoud any success. I have tried to rename the constant to "META-INF/config.properties" and still getting null.
Related
I want to create a file (outside of the workspace) so that everyone who opens my program has his own Textfile.
Currently I have to following Code:
private static final File m_dataFile = new File("C:\\temp\\MainPlayersLoginData.txt");
private static FileWriter writer;
private static Scanner reader;
public static void setMainPlayersLoginData(String name, String password) {
try {
if (!m_dataFile.exists()) {
createDirectory();
}
writer = new FileWriter(m_dataFile);
writer.write(name + "\n" + password);
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void createDirectory() {
System.out.println("creating directory: " + m_dataFile.getName());
boolean result = false;
try {
m_dataFile.mkdirs();
result = true;
} catch (SecurityException se) {
}
if (result) {
System.out.println("DIR created");
}
}
With this code, the program creates a folder temp as planned, but creates a folder named "MainPlayersLoginData.txt" in it instead of a textfile. In addition I get a FileNotFoundException with the message "Access denied" when initialising the FileWriter.
I tried using m_datafile.mkdir() instead of m_datafile.mkdirs() but this time I get a FileNotFoundException with the message "The system cannot find the specified path" and the folder temp isnt created.
Edit: If i create the folder and the Textfile on my own, everything works fine.
I want to retrieve the shared file url of an existing file on Dropbox. I am using the dropbox-java-sdk, and I have managed to create a shared link for a file I just uploaded. The only way I managed to get the shared link of an existing file is by listing all links and get the one I want depending on the path, like so
public void getShareLink(String path) throws DbxApiException, DbxException{
DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
try {
ListSharedLinksResult sharedLinkMetadata = client.sharing().listSharedLinks();
for (SharedLinkMetadata slm: sharedLinkMetadata.getLinks()){
if(slm.getPathLower().equalsIgnoreCase(path)){
System.out.println(slm.getUrl());
return;
}
}
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
} catch (DbxException ex) {
System.out.println(ex);
}
}
Isn’t there a way to directly get the url for the file I want? I just think it is a waste to iterate all items just to get one of them.
Get a ListSharedLinksBuilder from listSharedLinksBuilder and set ListSharedLinksBuilder.withDirectOnly to request only links for the exact path specified:
public String getShareLink(String path) {
DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
try {
ListSharedLinksResult sh = client.sharing().listSharedLinksBuilder()
.withPath(path)
.withDirectOnly(true)
.start();
for (SharedLinkMetadata slm : sh.getLinks()) {
return slm.getUrl();
}
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
return null;
} catch (DbxException ex) {
System.out.println(ex);
return null;
}
return null;
}
I have a method that sets a few properties using Java Properties if the config file does not exists in the local drive. However the bad thing I found out was even if I change a few properties in my code, the method just checks if the file exists and does not update the file with new properties.
The other condition is the user might have overwritten one of the property in the config file. So the method that sets the property should not overwrite the values on the config file with my code.
Here is what I did
private void setDefaultConfig() {
try {
if (!checkIfFileExists(configFile)) {
setProperty(configFile, "cfgFile", "cfg.xml");
setProperty(configFile, "type", "emp");
setProperty(configFile, "url", "www.google.com");
}
} catch (Exception e) {
e.printStackTrace();
}
}
setPropertyMethod just sets the property on the specified file.
Now if I add another property in my method the user won't get the new property because I'm just checking if the file exists.
For Eg : If the user changes the "url" property to have "www.yahoo.com" then my code in the setDefaultConfig method should not replace the value with "www.google.com"
Is there any method that can handle this situation?
protected void setProperty(String fileName, String property, String value) {
Properties prop = new Properties();
FileOutputStream fileOut = null;
FileInputStream fileIn = null;
try {
File file = new File(fileName);
if(checkIfFileExists(fileName)) {
fileIn = new FileInputStream(file);
prop.load(fileIn);
}
prop.setProperty(property, value);
fileOut = new FileOutputStream(fileName);
prop.store(fileOut, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Case: Have and messaging appln will be deployed to JBOSS 6.1.1 server. Have different queue names for different environments. Is there any way to have the queue names and the details read from a config file instead of Queuenames
Hard coded in Annotation
Defined in ejb-jar.xml
Referring in jboss Standalone.xml
Regards,
Sucheta
You can put properties file in the root of your server. Access it within FileInputStream and set it in your MDB class.
make a singleton class and read the properties from this class:
public class EnvironmentProperties {
private static final EnvironmentProperties INSTANCE = new EnvironmentProperties();
private Properties props = null;
private Log log = LogFactory.getLog(EnvironmentProperties.class);
private EnvironmentProperties() {
loadProperties();
}
public static EnvironmentProperties getInstance() {
return INSTANCE;
}
public String getJmsName() {
return props.getProperty("jms.name");
}
public String getJmsQueue() {
return props.getProperty("jms.queue");
}
private Object readResolve() {
return INSTANCE;
}
private Properties loadProperties() {
props = new Properties();
try {
String filePath = new File("./config.properties").getCanonicalPath();
FileInputStream fis = new FileInputStream(filePath);
props.load(fis);
fis.close();
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return props;
}
}
get access to jms/queue name : EnvironmentProperties.getInstance().getJmsName();
Make sure the properties file existed on all your servers
I have written the code bellow to check if a properties file exists and has the required properties. If it exists it prints the message that the file exists and is intact, if not then it creates the properties file with the required properties.
What I wanted to know is, is there a more elegant way of doing this or is my way pretty much the best way? Also the minor problem that I'm having is that with this way it doesn't check for extra properties that should not be there, is there a way to do that?
Summary of my requirements:
Check if the file exists
Check if it has the required properties
Check if it has extra properties
Create the file with the required properties if it doesn't exist or if there are extra or missing properties
Source files and Netbeans Project download
Source:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
public class TestClass {
public static void main(String[] args) {
File propertiesFile = new File("config.properties");
if (propertiesFile.exists() && propertiesExist(propertiesFile)) {
System.out.println("Properties file was found and is intact");
} else {
System.out.println("Properties file is being created");
createProperties(propertiesFile);
System.out.println("Properties was created!");
}
}
public static boolean propertiesExist(File propertiesFile) {
Properties prop = new Properties();
InputStream input = null;
boolean exists = false;
try {
input = new FileInputStream(propertiesFile);
prop.load(input);
exists = prop.getProperty("user") != null
&& prop.getProperty("pass") != null;
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return exists;
}
public static void createProperties(File propertiesFile)
{
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream(propertiesFile);
prop.setProperty("user", "username");
prop.setProperty("pass", "password");
// save properties to project root folder
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
this is my way. (not sure if more elegant, but it can be an inspiration/different aproach)
Try/catch should be enough to see if the file exists or not
try {
loading files etc...
} catch (FileNotFoundException e) {
throw new MojoExecutionException( "[ERROR] File not found", e );
} catch (IOException e) {
throw new MojoExecutionException( "[ERROR] Error reading properties", e );
}
Code that checks your loaded prop:
Properties tmp = new Properties();
for(String key : prop.stringPropertyNames()) {
if(tmp.containsKey(key)){
whatever you want to do...
}
}
I use tmp, a new properties variable, to compare with ,but the key variable will hold a string so in the if statement you can compare it to array of strings and the way you do it is up to you.