get values stored in the properties file

1
0
-1

Hi,

Can someone help me, please..

I create a properties file in java and import in bonita as jar file, add the jar in process dependency.

When I create a variable in process level the initialize work great

String filename = "NOMBRE_ARCHIVO.properties";
Properties properties = new Properties();
InputStream is = null;
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
properties.load(is);
return properties;

But when I tried get the values of properties I get a error "variable that you try to access is unreachable".

My properties file It contains a database configuration; user name, password, database name.

I tried

properties.getProperty("password");

this don't work for me I use this in a process step, my first step, because I want a list of the database table.

regards..

1 answer

1
+2
-1

I offer the following (Groovy) code that reads a properties file for the configuration etc.

In it I also use a include from jasypt.org for encrypted properties file. Please see their website for more details.

The config file must be placed in directory "/webapps/myCompany/properties/" and would be formatted as follows:

ldap.lDAPServer=put.your.hostname.here
ldap.lDAPPort=389
ldap.bindString=CN=put,OU=your,OU=full,OU=bind,DC=user,DC=dn,DC=here
ldap.bindPassword=binduserpasswordgoeshere
ldap.baseDN=dc=enduser, dc=base, dc=dn
ldap.userBindDN=

an encrypted version of the above file could look like:

ldap.lDAPServer=ENC(sknva;uhga;kjgbs41fg)
ldap.lDAPPort=ENC(fghfsdbf7575)
ldap.bindString=ENC(7686fkrhfgis7fgsdhg7htlsghlsrs5ths8yghgliitg78hslghhgl578thsl)
ldap.bindPassword=ENC(zkbfkvuygr7gha7tyafbhdflbas78tyaufh2983208rz.uziluhgzr7gh)
ldap.baseDN=ENC(zjhbvzkvbz7vl;a84th;afubvzlbhtg84h;t;s;uvhbz;rg)
ldap.userBindDN=ENC(sdfdfs)

The Groovy properties code, which is really a subroutine I use to pull text from language files (if you add this to Development->Manage Groovy Scripts you can use it is your scripts to get get from multiple properties files using String myString = myGroovyText.getProperty("myTextKey");), **must **be modified to work in the ldap code above:

/**
  * Copyright (C) 2015 Gubernare Ltd.,
  * London, United Kingdom
  *
  * This code is free software; you can redistribute it and/or modify it under the terms
  * of the GNU Lesser General Public License as published by the Free Software Foundation
  * version 2.1 of the License.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License along with this
  * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  * Floor, Boston, MA 02110-1301, USA.
  */

 
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.properties.EncryptableProperties;
 
import java.util.regex.Pattern;
 
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
 
def static getPropertyString(String key){
 
Logger logger= Logger.getLogger("org.bonitasoft");
int d = 0;
boolean debug = false; //change to true for logging
 
//preample code - preparation
//get catalina home - can sometimes NOT be set so work it through...
def thisModule = " getPropertyString: ";
String catalinaHome = System.getProperty("CATALINA_HOME");
String catalinaPath = System.getProperty("CATALINA_PATH");
if(debug){d++; logger.severe(d+thisModule+": CATALINA_HOME: "+catalinaHome);}
if(debug){d++; logger.severe(d+thisModule+": CATALINA_PATH: "+catalinaPath);}
 
if(debug){d++; logger.severe(d+thisModule+": if (catalinaHome == null && catalinaPath == null){");}
if (catalinaHome == null && catalinaPath == null){
 
if(debug){d++; logger.severe(d+thisModule+": CATASTOPHIC ERROR CATALINA Not found: ");}
 
String strClassPath = System.getProperty("java.class.path");
if(debug){d++; logger.severe(d+thisModule+": strClassPath: "+strClassPath);}
//20150201 new version to get to webapps - START
if (strClassPath.indexOf(";") != 0 ){
 
//strClassPath is concatenated and must be reduced to one dir
if(debug){d++; logger.severe(d+thisModule+"strClassPath - is concatenated?");}
 
String[] catHome = strClassPath.split(Pattern.quote(";"));
for (String singleCatHome : catHome){
 
if(debug){d++; logger.severe(d+thisModule+"singleCatHome : "+singleCatHome);}
if (singleCatHome.indexOf("bin") != 0){
String noBin = singleCatHome.substring(0, singleCatHome.indexOf("bin"));
 
File f = new File(noBin+"webapps");
if(debug){d++; logger.severe(d+thisModule+"noBin (webapps): "+noBin+"webapps");}
if (f.exists() && f.isDirectory()) {
if(debug){d++; logger.severe(d+thisModule+"singleCatHome Found webapps : "+noBin+"webapps");}
catalinaHome = noBin;
break;
}
}
}
}
else{
//strClassPath is not concated and is OK?
}
//20150201 new version to get to webapps - END
 
}
else if (catalinaHome == null && catalinaPath != null){
if(debug){d++; logger.severe(d+thisModule+"Set Home = Path");}
catalinaHome = catalinaPath;
}
else { // we have a CatalinaHome - but is it concatenated?
if(debug){d++; logger.severe(d+thisModule+"CatalinaHome - but is it concatenated?");}
 
if (catalinaHome.indexOf(";") != 0 ){
//catalinaHome is concatenated and must be reduced to one dir
if(debug){d++; logger.severe(d+thisModule+"CatalinaHome - is concatenated?");}
 
String[] catHome = catalinaHome.split(Pattern.quote(";"));
for (String singleCatHome : catHome){
if(debug){d++; logger.severe(d+thisModule+"CatalinaHome : "+singleCatHome);}
if (singleCatHome.indexOf("webapps") != 0){
if(debug){d++; logger.severe(d+thisModule+"CatalinaHome Found webapps : "+singleCatHome);}
catalinaHome = singleCatHome;
break;
}
}
}
else{
//catalinaHome is not concated and is OK
}
}
 
//set encryption
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword("myPassword");
Properties propsEnc = new EncryptableProperties(encryptor);
 
//set locale - defaults
String defaultLang = "en";
String defCountry = "US";
 
Locale locale = Locale.getDefault();
String lang = locale.getLanguage();
String country = locale.getCountry();
 
String propertiesDir = "/webapps/myCompany/properties/";
String fileNameEncrypted = "fileNameEncrypted_";
String fileNameNotEncrypted = "fileNameNotEncrypted_";
 
String propertiesFileEncLocale = propertiesDir+fileNameEncrypted+lang+"_"+country+".properties";
String propertiesFileEncDefLoc = propertiesDir+fileNameEncrypted+defaultLang+"_"+defCountry+".properties";
String propertiesFileEnc = propertiesDir+fileNameEncrypted+".properties";
String propertiesFileLocale = propertiesDir+fileNameNotEncrypted+lang+"_"+country+".properties";
String propertiesFileDefLoc = propertiesDir+fileNameNotEncrypted+defaultLang+"_"+defCountry+".properties";
String propertiesFile = propertiesDir+fileNameNotEncrypted+".properties";
 
 
try{ //1
try{ //encrypted locale
if(debug){d++; logger.severe(d+thisModule+": try: "+catalinaHome + propertiesFileEncLocale);}
propsEnc.load(new FileInputStream(new File(catalinaHome + propertiesFileEncLocale)));
}
catch(Exception e0){
try{ //encrypted default
if(debug){d++; logger.severe(d+thisModule+": try: "+catalinaHome + propertiesFileEncDefLoc);}
propsEnc.load(new FileInputStream(new File(catalinaHome + propertiesFileEncDefLoc)));
}
catch(Exception e1){
try{ //encrypted
if(debug){d++; logger.severe(d+thisModule+": try: "+catalinaHome + propertiesFileEnc);}
propsEnc.load(new FileInputStream(new File(catalinaHome + propertiesFileEnc)));
}
catch(Exception e2){
try{ // locale
if(debug){d++; logger.severe(d+thisModule+": try: "+catalinaHome + propertiesFileLocale);}
propsEnc.load(new FileInputStream(new File(catalinaHome + propertiesFileLocale)));
}
catch(Exception e3){
try{ // locale
if(debug){d++; logger.severe(d+thisModule+": try: "+catalinaHome + propertiesFileDefLoc);}
propsEnc.load(new FileInputStream(new File(catalinaHome + propertiesFileDefLoc)));
}
catch(Exception e4){
try{ // default
if(debug){d++; logger.severe(d+thisModule+": try: "+catalinaHome + propertiesFile);}
propsEnc.load(new FileInputStream(new File(catalinaHome + propertiesFile)));
}
catch(Exception e5){
logger.severe(thisModule+": fileNameNotEncrypted.properties Error (E5-0): File Not Found: " + e5.toString());
logger.severe(thisModule+": fileNameNotEncrypted.properties Error (E5-1): File Not Found: " + +catalinaHome + propertiesFile.toString());
return "Error (e5): fileNameNotEncrypted.properties Error (E5): File Not Found";
}
}
}
}
}
}
 
try{
if(debug){d++; logger.severe(d+thisModule+": Key: "+key);}
return propsEnc.getProperty(key);
}
catch(Exception ex3){
logger.severe(thisModule+": Error (ex6): Message Not Found: "+key);
return "Error (ex6): Message Not Found: "+key;
}
 
}
catch(Exception ex0){
logger.severe(thisModule+": Fatal Error (EX0): File Not Found: " + ex0.toString());
return ": Fatal Error (EX0): File Not Found: ";
}

Hope it helps, regards

Seán

Notifications