(opendj-ldap-sdk-2.6.0) bind method parameter - password char [ ] - java

I'm using opendj-ldap-sdk-2.6.0 jar library to search LDAP entry.
I am following the guide.
(https://backstage.forgerock.com/docs/opendj/2.6/dev-guide/#chap-using-the-sdk)
source code :
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
import org.forgerock.opendj.ldap.SearchScope;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldap.responses.SearchResultReference;
import org.forgerock.opendj.ldif.ConnectionEntryReader;
import org.forgerock.opendj.ldif.LDIFEntryWriter;
public class Test {
public static void main(String[] args) {
final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
Connection connection = null;
try {
final LDAPConnectionFactory factory = new LDAPConnectionFactory("localhost",389);
connection = factory.getConnection();
connection.bind("cn = Directory Mangager", password );
// password is just an example of the password.
final ConnectionEntryReader reader = connection.search("dc=example,dc=com", SearchScope.WHOLE_SUBTREE,"(uid=bjensen)","*");
while (reader.hasNext()) {
if(reader.isEntry()) {
final SearchResultEntry entry = reader.readEntry();
writer.writeComment("Search result entry:" + entry.getName().toString());
writer.writeEntry(entry);
} else {
final SearchResultReference ref = reader.readReference();
writer.writeComment("Search result reference:" + ref.getURIs().toString());
}
}
writer.flush();
} catch (final Exception e) {
System.err.println(e.getMessage());
} finally {
if (connection !=null) {
connection.close();
}
}
}
connection.bind("cn = Directory Mangager", password );
I'm getting a red line at this line under password because the parameter has to be 'char []'.
I captured Bind method in the below.
If my password is 1234, how can I change that into char [] type?

You're missing a call from factory to obtain a connection.
connection = factory.getConnection();
connection.bind("cn = Directory Mangager", password );

I figured it out.
connection.bind("cn=Directory Manager", "yourpassword".toCharArray() );
You can use toCharArray()
Also, as Ludovic Poitou mentioned above, you need to use
connection = factory.getConnection(); with the bind method.
The guide says if you are not using anonymous search, use the bind method, but you gotta use them both. (I misunderstood the guide)

Related

How to implement Configurations class in Java?

I am using this example to read from configuration file (data such as host name, password, etc) . But they did not include the Configurations class itself.
So I am not really sure how that should be implemented.
Here is how I am trying to read the properties from Main class:
Configurations configs = new Configurations(); // Error: cannot find symbol symbol: class Configurations location: class Main
try {
Configuration config = configs.properties(new File("database.properties"));
String dbHost = config.getString("database.host");
int dbPort = config.getInt("database.port");
String dbUser = config.getString("database.user");
String dbPassword = config.getString("database.password", "secret"); // provide a default
long dbTimeout = config.getLong("database.timeout");
} catch (ConfigurationException cex) {
cex.printStackTrace();
}
And this is how my database.properties file looks:
database.host = "dbname";
datatabase.port = 5005;
datatabase.user = "root";
datatabase.password = "";
database.timeout = 60000
P.S. Sorry for my stupidity, I am very new to Java.
You can use the properties class in java, which has a load method that specifies an inputstream.
Then, you can read your properties file via FileInputStream.
example:
public class Test {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
InputStream inputStream =
new FileInputStream("D:\\work_space\\java_workspace\\test-mq\\src\\main\\resources\\database.properties");
properties.load(inputStream);
String host = properties.getProperty("database.host");
// get more properties......
System.out.println(host);
}
}

hadoop distributed copy overwrite not working

I am trying to use the org.apache.hadoop.tools.DistCp class to copy some files over into a S3 bucket. However overwrite functionality is not working in spite of explicitly setting the overwrite flag to true
Copying works fine but it does not overwrite if there are existing files. The copy mapper skips those files. I have explicitly set the "overwrite" option to true.
import com.typesafe.scalalogging.LazyLogging
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
import org.apache.hadoop.tools.{DistCp, DistCpOptions}
import org.apache.hadoop.util.ToolRunner
import scala.collection.JavaConverters._
object distcptest extends App with LazyLogging {
def copytoS3( hdfsSrcFilePathStr: String, s3DestPathStr: String) = {
val hdfsSrcPathList = List(new Path(hdfsSrcFilePathStr))
val s3DestPath = new Path(s3DestPathStr)
val distcpOpt = new DistCpOptions(hdfsSrcPathList.asJava, s3DestPath)
// Overwriting is not working inspite of explicitly setting it to true.
distcpOpt.setOverwrite(true)
val conf: Configuration = new Configuration()
conf.set("fs.s3n.awsSecretAccessKey", "secret key")
conf.set("fs.s3n.awsAccessKeyId", "access key")
conf.set("fs.s3n.impl", "org.apache.hadoop.fs.s3native.NativeS3FileSystem")
val distCp: DistCp = new DistCp(conf, distcpOpt)
val filepaths: Array[String] = Array(hdfsSrcFilePathStr, s3DestPathStr)
try {
val distCp_result = ToolRunner.run(distCp, filepaths)
if (distCp_result != 0) {
logger.error(s"DistCP has failed with - error code = $distCp_result")
}
}
catch {
case e: Exception => {
e.printStackTrace()
}
}
}
copytoS3("hdfs://abc/pqr", "s3n://xyz/wst")
}
I think the problem is you called ToolRunner.run(distCp, filepaths).
If you check the source code of DistCp, in run method will overwrite inputOptions, so the DistCpOptions passed to constructor will not work.
#Override
public int run(String[] argv) {
...
try {
inputOptions = (OptionsParser.parse(argv));
...
} catch (Throwable e) {
...
}
...
}

Table_not_available when using JAVA SAP RFC_READ_TABLE

There are three different tables (OPTIONS, FIELDS and DATA) in import parameter "QUERY_TABLE" = "LTAP".
I created a java program to display column FIELDNAME from the table FIELDS with helping function RFC_READ_TABLE.
It always appears Error com.sap.conn.jco.AbapException: (126) TABLE_NOT_AVAILABLE: TABLE_NOT_AVAILABLE Message 300 of class DA type E, when I call the method step2WorkWithTable().
Can anybody explain the error? And how to fix it?
My codes:
import java.util.Properties;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
public class RFC_Read_Table {
public static void main(String[] args) throws JCoException
{
System.out.println("Step1: connect SAP without Pool");
step1Connect();
System.out.println("");
System.out.println("Step2: call RFC_Read_Table ");
step2WorkWithTable();
System.out.println("--------------------------------");
System.out.println("finished");
}
static {
String DESTINATION_NAME1 = "mySAPSystem";
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ABC");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "33");
connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, "/A/123/");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "UserID");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "Passwort");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "de");
createDestinationDataFile(DESTINATION_NAME1, connectProperties);
}
private static void createDestinationDataFile(String destinationName, Properties connectProperties) {
File destCfg = new File(destinationName+".jcoDestination");
try
{
FileOutputStream fos = new FileOutputStream(destCfg, false);
connectProperties.store(fos, "for tests only !");
fos.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to create the destination files", e);
}
}
public static void step1Connect() throws JCoException
{
try {
JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
System.out.println("connected");
destination.ping();
} catch (JCoException e) {
e.printStackTrace();
System.out.println("not connected");
}
}
public static void step2WorkWithTable() throws JCoException
{
JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
JCoFunction function = destination.getRepository().getFunction("RFC_READ_TABLE");
if (function == null)
throw new RuntimeException("RFC_Read_Table not found in SAP.");
try
{
function.execute(destination);
}
catch(AbapException e)
{
System.out.println(e.toString());
return;
}
function.getImportParameterList().setValue("QUERY_TABLE","LTAP");
JCoTable codes = function.getTableParameterList().getTable("FIELDS");
codes.appendRow();
for (int i = 0; i < codes.getNumRows(); i++)
{
codes.setRow(i);
System.out.println(codes.getString("FIELDNAME"));
}
codes.firstRow();
for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow())
{
function = destination.getRepository().getFunction("RFC_READ_TABLE");
if (function == null)
throw new RuntimeException("RFC_READ_TABLE not found in SAP.");
function.getImportParameterList().setValue("FIELDNAMEID", codes.getString("FIELDNAME"));
try
{
function.execute(destination);
}
catch (AbapException e)
{
System.out.println(e.toString());
return;
}
JCoStructure detail = function.getExportParameterList().getStructure("FIELDS");
System.out.println(detail.getString("FIELDNAME"));
}
}
}
There is nothing wrong with your JCo code. The error message comes from the SAP system. So you need to check in the SAP system, what that error code means. This can be done in transaction SE91. You enter message class = "DA" and message number = "300" and click display.
I did this for you, and the result is:
"No active nametab exists for &"
where '&' needs to be replaced by the input, which is "LTAP" in this case. So we have "No active nametab exists for LTAP".
This error basically means: the database table "LTAP" exists on the database, but has not yet been activated in the ABAP DDIC. (Perhaps because it still contains a syntax error, or a required data element/domain is missing, etc.)
Solution: go to transaction SE11 and try to activate the table. This will probably give you an error message about what is wrong with this table. Fix all the syntax errors, activate it, and then you can use it.
Note: if LTAP is a standard table delivered by SAP, this error probably means that something went wrong when installing a transport/hotpackage from SAP that contained modifications to this table. In this case you should better contact SAP support to get the table back into a "consistent" state again.
I see you are connecting to an ABAP system using JCoDestinationManager. It means your are using the properties from mySAPSystem destination. Please check if the mySAPSystem connects to a proper ABAP system.
What are these lines are needed for
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ABC");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "33");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "UserID");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "Passwort");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "de");
I don't see them to be used anywhere in your program. It seems their are not applied to your connection...

Exception in thread "main" java.lang.ExceptionInInitializerError while trying to run ST312_TestMain class

I am trying to call "getOrderList" from the ST312_TestMain class. I am getting the java.lang.ExceptionInInitializerError for the below mentioned class.
package com.Main;
import org.w3c.dom.Document;
import com.yantra.ycp.core.YCPContext;
import com.Main.XMLUtil;
import com.Main.SterlingUtil;
public class ST312_TestMain {
public static void main(String[] args) throws Exception {
String ServiceName = "getOrderList";
String sServiceFlag = "N";
Document dTemplate = null;
//ServiceName = "SendDN";
//sServiceFlag = "Y";
Document inputXML=null;
inputXML = XMLUtil.getDocument("<Order OrderHeaderKey='201407181105267340509' />");
//inputXML = XMLUtil.getXmlFromFile("src/Test.xml");
dTemplate = XMLUtil.getDocument("<Order OrderHeaderKey='' OrderNo=''/>");
if (args.length == 3) {
ServiceName = args[0];
sServiceFlag = args[1].equals("Y") ? "Y" : "N";
inputXML = XMLUtil.getXmlFromFile(args[2]);
} else {
System.out
.println("Usage: TestMain <API/Service Name> <API/Service(N/Y)> <Input XML File>");
System.out
.println("No Input received using preset XML to call preset Service");
System.out.println("Service Name=" + ServiceName);
}
***YCPContext env = new YCPContext("admin", "admin");***
System.out.println("Input XML \n" + XMLUtil.getXmlString(inputXML));
try {
Document outputXML = null;
if ("Y".equals(sServiceFlag)) {
outputXML = SterlingUtil.callService(env, inputXML, ServiceName, null);
} else {
outputXML = SterlingUtil.callAPI(env, inputXML, ServiceName, dTemplate);
}
env.commit();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Service Invocation Failed");
}
}
}
The exception is as follows:
Usage: TestMain <API/Service Name> <API/Service(N/Y)> <Input XML File>
No Input received using preset XML to call preset Service
Service Name=getOrderList
log4j:WARN No appenders could be found for logger (com.yantra.ycp.core.YCPContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.sterlingcommerce.woodstock.util.frame.Manager.getProperty(Manager.java:1365)
at com.yantra.yfc.util.YFCConfigurator.setStandalone(YFCConfigurator.java:37)
at com.yantra.yfs.core.YFSSystem.init(YFSSystem.java:62)
at com.yantra.yfs.core.YFSSystem.<clinit>(YFSSystem.java:47)
at com.yantra.ycp.core.YCPContext.<init>(YCPContext.java:288)
at com.yantra.ycp.core.YCPContext.<init>(YCPContext.java:276)
at com.Main.ST312_TestMain.main(ST312_TestMain.java:31)
Caused by: java.lang.NullPointerException
at com.sterlingcommerce.woodstock.util.frame.log.base.SCILogBaseConfig.doConfigure(SCILogBaseConfig.java:35)
at com.sterlingcommerce.woodstock.util.frame.log.LogService.<clinit>(LogService.java:110)
... 7 more
Please help me on this problem, since I am not sure how to handle the YCPContext object. ("YCPContext env = new YCPContext("admin", "admin");"). Thanks in advance.
Request support from IBM about this.
No matter what mistakes you may have made configuring it (if any), the Sterling code should not be throwing an NPE at you.
If you are testing in local using main() method, then you should comment all the lines where you are using environment varilable. For your code its env variable.
Probably you wouldn't have added the database driver jars to your project build path... For e.g.
For DB2 database, db2jcc.jar has to be added
For Oracle database, ojdbc.jar has to be added

Making connection to oracle database using properties file

The program two.java is compiling but no output is being produced ,no exception occuring .
(Executing in cmd)
//db.properties
driverclass = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:#loacalhost:1521:xe
user = system
password = kapil
//ConnectionProvider.java
class ConnectionProvider
{
static Properties prop;
static
{
prop = new Properties();
String path = File.separator + "db.properties";
InputStream in = prop.getClass().getResourceAsStream(path);
try
{
prop.load(in);
}
catch(Exception e)
{
}
}
public static Connection getConnection() throws Exception
{
Class.forName(prop.getProperty("driverclass"));
Connection con = DriverManager.getConnection(
prop.getProperty("url"),
prop.getProperty("user"),
prop.getProperty("password"));
return con;
}
}
// two.java
class Two
{
public static void main(String args[])
{
try
{
Connection con = ConnectionProvider.getConnection();
Statement stmt = con.createStatement();
ResultSet rset = stmt.executeQuery("Select * from Emp ");
while(rset.next())
{
System.out.println(rset.getInt(1) + "\t"
+ rset.getString(2) + "\t"
+ rset.getString(3) + "\t"
+ rset.getInt(4));
}
con.close();
}
catch(Exception e){}
}
}
First thing dont consume the exception by doing this catch(Exception e){} Its not a good practice always print the stacktrace like the catch(Exception e){ e.printStacktrace();}
now the problem in ur code is the url change it to-
url = jdbc:oracle:thin:#localhost:1521:xe
there is a typo in url's localhost.
Edit :As you are executing the class through cmd I expect that the classes and the db.properties are in same folder try something like this
try {
prop.load(new FileInputStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
so the full ConnectionProvider class looks something like this
class ConnectionProvider
{
static Properties prop;
static
{
prop = new Properties();
try {
prop.load(new FileInputStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception
{
Class.forName(prop.getProperty("driverclass"));
Connection con = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("user"), prop.getProperty("password"));
return con;
}
}
Now while executing the class dont forget to include the ojdbc6.jar in your classpath.You can get it from here.
Exception is not occurred because you are catching it but not printing it. In other words, exception might occur but you are not printing it.
add this to your catch block e.printStackTrace();
Also this code:
String path = File.separator + "db.properties"; doesn't seem correct.
First print out the path to make sure you are pointing at correct file.
You can use .getCanonicalPath() to get absolute path.
Do something like this:
String filePath = new File("./yourfile.properties").getCanonicalPath();
FileInputStream fis = new FileInputStream(filePath);
props.load(fis);
You can use BalusC's great dao layer tutorial. There is an properties file loader that will fit your needs. Summary of the code is below.
This is how the file dao.properties will look like (change jdbc.url and jdbc.driver for Oracle Db):
javabase.jdbc.url = jdbc:mysql://localhost:3306/javabase
javabase.jdbc.driver = com.mysql.jdbc.Driver
javabase.jdbc.username = java
javabase.jdbc.password = d$7hF_r!9Y
Properties file loader (as noted you may change it depending on your needs, Note: it depends on how often you think that this file changes in your environment, if it changes only once per year, then it is really not worth that to load it from disk everytime, but if it changes for example every day, then it might be worth to add a static method which reloads the properties file and execute it by some (scheduled) background job.)
package com.example.dao;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* This class immediately loads the DAO properties file 'dao.properties' once in memory and provides
* a constructor which takes the specific key which is to be used as property key prefix of the DAO
* properties file. There is a property getter which only returns the property prefixed with
* 'specificKey.' and provides the option to indicate whether the property is mandatory or not.
*
* #author BalusC
* #link http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html
*/
public class DAOProperties {
// Constants ----------------------------------------------------------------------------------
private static final String PROPERTIES_FILE = "dao.properties";
private static final Properties PROPERTIES = new Properties();
static {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE);
if (propertiesFile == null) {
throw new DAOConfigurationException(
"Properties file '" + PROPERTIES_FILE + "' is missing in classpath.");
}
try {
PROPERTIES.load(propertiesFile);
} catch (IOException e) {
throw new DAOConfigurationException(
"Cannot load properties file '" + PROPERTIES_FILE + "'.", e);
}
}
// Vars ---------------------------------------------------------------------------------------
private String specificKey;
// Constructors -------------------------------------------------------------------------------
/**
* Construct a DAOProperties instance for the given specific key which is to be used as property
* key prefix of the DAO properties file.
* #param specificKey The specific key which is to be used as property key prefix.
* #throws DAOConfigurationException During class initialization if the DAO properties file is
* missing in the classpath or cannot be loaded.
*/
public DAOProperties(String specificKey) throws DAOConfigurationException {
this.specificKey = specificKey;
}
// Actions ------------------------------------------------------------------------------------
/**
* Returns the DAOProperties instance specific property value associated with the given key with
* the option to indicate whether the property is mandatory or not.
* #param key The key to be associated with a DAOProperties instance specific value.
* #param mandatory Sets whether the returned property value should not be null nor empty.
* #return The DAOProperties instance specific property value associated with the given key.
* #throws DAOConfigurationException If the returned property value is null or empty while
* it is mandatory.
*/
public String getProperty(String key, boolean mandatory) throws DAOConfigurationException {
String fullKey = specificKey + "." + key;
String property = PROPERTIES.getProperty(fullKey);
if (property == null || property.trim().length() == 0) {
if (mandatory) {
throw new DAOConfigurationException("Required property '" + fullKey + "'"
+ " is missing in properties file '" + PROPERTIES_FILE + "'.");
} else {
// Make empty value null. Empty Strings are evil.
property = null;
}
}
return property;
}
}

Categories

Resources