cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

We are happy to announce the new Windchill Customization board! Learn more.

Standalone code sample that does user authentication & remote access to services & manager

MartyRoss
1-Newbie

Standalone code sample that does user authentication & remote access to services & manager

Can anyone offer up a "standalone" (i.e., uses "main", not an applet) java source code sample that remotely authenticates a user with a password and uses server side code via helper objects (eventually looking towards walking product structure, e.g., from what I understand, using wt.fc.PersistenceHelper.manager, wt.vc.struct.StructHelper.service, ...)

9 REPLIES 9
avillanueva
22-Sapphire I
(To:MartyRoss)

Here is a simple one, renaming WTParts via a tab delimited text file
(number and new name). You need to source the codebase and some other
jars when you run this. Will prompt for authentication.



/*

* RenameJob.java

*

* Created on August 9, 2005, 10:37 AM

*/



/*

* RenameJob.java

*

* Created on August 23, 2004

*/



package customization.cmdJobs;

import java.io.*;

import wt.fc.*;

import wt.query.QuerySpec;

import wt.util.*;

import wt.query.SearchCondition;

import java.util.*;

import wt.part.*;

import org.apache.log4j.*;



/**

*

* @author Antonio.Villanueva

*/

public class RenameJobWTPart {

public Logger logger=Logger.getRootLogger();



/** Creates a new instance of MovePartstoDocsJob */

public RenameJobWTPart() {

initLogging();

}



private void initLogging() {

Appender appender;

appender = new ConsoleAppender(new PatternLayout("%d{ISO8601}
%p-%m\n"),ConsoleAppender.SYSTEM_OUT);

logger.addAppender(appender);

logger.setLevel(Level.DEBUG);

}





public static void main (String[] args) throws wt.util.WTException,
wt.util.WTPropertyVetoException, ClassNotFoundException

{

System.out.println("Connected to PDMLink!");

RenameJobWTPart prj=new RenameJobWTPart();



try

{

//load data file

BufferedReader br = new BufferedReader(new InputStreamReader(new
FileInputStream(args[0])));



String line;

boolean readNumber=false;

boolean readName=false;

while((line = br.readLine()) != null)

{

StringTokenizer token=new StringTokenizer(line,"\t");

String number=token.nextToken();

readNumber=true;

prj.logger.debug("Read " + number);

String name=token.nextToken();

prj.logger.debug("Read " + name);

readName=true;

try {



QuerySpec queryspec = new QuerySpec(WTPartMaster.class);

queryspec.appendWhere(new
SearchCondition(WTPartMaster.class,WTPartMaster.NUMBER,SearchCondition.E
QUAL,number.toUpperCase()));

QueryResult
result=PersistenceHelper.manager.find(queryspec);

if (result.size()==1)

{

prj.logger.info("Found master of " + number);



WTPartMaster
master=(WTPartMaster)result.nextElement();

IdentificationObject identificationobject =
((Identified)master).getIdentificationObject();

WTPartMasterIdentity
identity=(WTPartMasterIdentity)identificationobject;

identity.setName(name);


IdentityHelper.service.changeIdentity((Identified)master,identity);

prj.logger.info("Identity Changed");



}

else

prj.logger.error("WTPart not found: " + number);

}

catch (WTException e)

{

prj.logger.error("Error during query", e);

}

}

}

catch (IOException e)

{

prj.logger.error("Error loading data file", e);

}





System.exit(0);



}



}




Thank you, Antonio.

How do you point this client at the Windchill server?

I'm also looking for a way to have this program submit the login credentials without user interaction.

avillanueva
22-Sapphire I
(To:MartyRoss)

It picks it up from the codebase/wt.properties file


How does it locate codebase/wt.properties on the server? I'm trying to run this on a client machine with no Windchill installed on it.

There must be someinitialization that I'm missing.

For instance, the following example program generates the same exception that I'm getting in the main program I'm trying to create.

All it does is instantiate a "QuerySpec", something that your program appears to do without any special initialization.

  package org.hphc.mdr.play;
  import wt.query.QueryException;
  import wt.query.QuerySpec;
  public class GetQuerySpec {
    public static void main(String[] args) throws QueryException {
      QuerySpec querySpec = new QuerySpec();
    } 
  }

When I run the above, I get:

Exception in thread "main" java.lang.ExceptionInInitializerError

at wt.query.QuerySpec.<clinit>(
<u>QuerySpec.java:129</u>)at org.hphc.mdr.play.GetQuerySpec.main(<u>GetQuerySpec.java:9</u>)

Caused by: <u>java.lang.NullPointerException</u>

at wt.services.applicationcontext.ApplicationContextFactory.createAdapter(<u>ApplicationContextFactory.java:258</u>)

at wt.services.applicationcontext.ApplicationContextFactory.createNamedApplicationContext(<u>ApplicationContextFactory.java:242</u>)at wt.services.applicationcontext.ApplicationContextFactory.getDefaultApplicationContextServices(

<u>ApplicationContextFactory.java:164</u>)

at wt.dataservice.DataServiceFactory.<init>(<u>DataServiceFactory.java:68</u>)at wt.dataservice.DataServiceFactory.<init>(

<u>DataServiceFactory.java:63</u>)

at wt.dataservice.DataServiceFactory.getDefault(<u>DataServiceFactory.java:37</u>)at wt.pom.DBProperties.<clinit>(

<u>DBProperties.java:224</u>)

... 2 more

Try the following code to pass the credentials


import wt.method.RemoteMethodServer;

RemoteMethodServer methodServer = RemoteMethodServer.getDefault();
methodServer.setUserName("wcadmin");
methodServer.setPassword("wcadmin");


Regards,
Prathap <">http://goo.gl/LuT5>



Interesting!

I found out that if I copy ALL properties from the server to the client, things start working (though the following code fragment takes a LONG time to load everything from the server before it returns! - I'll be looking to pare this down to the bare minimum required, or to find an alternate method):

WTProperties remoteWTProperties = WTProperties.getServerProperties(new URL(WT_INSTANCE_URL));
WTProperties localWTProperties = WTProperties.getLocalProperties();
localWTProperties.putAll(remoteWTProperties);

Also, Prathap's suggestion about how to enter user credentials without user interaction apparently requires:

Authentication.setAllowUserInteraction(false);
avillanueva
22-Sapphire I
(To:MartyRoss)

You need to copy the codebase to your client machine or make available
via some share drive. Call it out in your classpath like this:



java -cp
R:\Configuration_Management\PDMLinkTools;\\someSystem\pdm-codebase;\\som
eSystem\pdm-codebase\WEB-INF\lib\ie.jar;\\someSystem\pdm-codebase\lib\se
rvlet.jar;\\someSystem\pdm-codebase\lib\wc3rdpartylibs.jar;\\someSystem\
pdm-codebase\lib\HTTPClient.jar;\\someSystem\pdm-codebase\lib\commons-co
llections.jar;\\someSystem\pdm-codebase\WEB-INF\lib\\wncWeb.jar;\\someSy
stem\pdm-codebase\WEB-INF\lib\\ieWeb.jar;lib\log4j-1.2.14.jar
customization.cmdJobs.RenameJobWTPart %1


Antonio,

Do you exclude introspections/info and the rest of the conf/property
files?

Thanks,
Dmitry
RussPratt
5-Regular Member
(To:MartyRoss)

FYI, when it comes to copying properties files to client machines, you should be aware that there may be information in those files that can be used to compromise system and data security. You're playing with fire.

Russ

Top Tags