標準サービスクラスの作成
標準サービスクラスを以下に保存します。
<Windchill>/src/com/acme/listen/StandardListenService.java
以下の例は、変更するべきでないいくつかの標準コードを示すコメントを含んでいます。これらは標準サービスマネジャーやその他のサービスと対話するために必要です。たとえば、選択したパッケージやクラス名の反映、および目的のイベントを受信するために、変更する必要のあるいくつかのコードを示すコメントもあります。ソースコードを以下に示します。
/* Change to your package name */
package com.acme. listen;
/* javac -classpath z:/Windchill/codebase -d z:/Windchill/codebase *.java */
/* xconfmanager —t codebase/wt.properties -s
wt.services.service.4160=com.acme.listen.Listenservice/com.acme.listen.StandardList
enservice -p */
/* Change to import you Service Interface */
import com.acme.listen.ListenService;
import java.lang.String;
/* Standard imports for services */
import wt.services.ManagerException;
import wt.services.ServiceEventListenerAdapter;
import wt.services.StandardManager;
import wt.session.SessionContext;
import wt.session.SessionHelper;
import wt.session.SessionServerHelper;
import wt.util.DebugFlag;
import wt.util.DebugProperties;
import wt.util.DebugType;
import wt.util.DebugWriter;
import wt.util.WIException;
import wt.util.WTMessage;

/* Changes required here:
* Imports necessary for listening to particular events, in this case
* pre delete events */
import wt.fc.PersistenceManagerEvent;
import wt.fc.collections.WTCollection;
/**
* Listens for pre delete event. When an event is
* "heard", prints messages to the MethodServer log and console.
* <p>
* Use the <code>newStandardlListenService</code> static factory method (s),
* not the <code>StandardListenService</code> constructor, to construct
* instances of this class. Instances must be constructed using the static
* factory(s), in order to ensure proper initialization of the instance.
* <p>
**/
/* Change to your class name */
public final class StandardListenService extends StandardManager
implements ListenService {
/* Standard class identification string */
private static final String CLASSNAME =
StandardListenService.class.getName () ;
/* Standard debug variables for services */
private static final boolean DEBUG = DebugProperties.isDebugOn (CLASSNAME) ;
private static final DebugWriter LOG =
(DEBUG ? DebugProperties.getWriter (CLASSNAME) : null);
/* Standard getter for class name */
public String getConceptualClassname() {
return CLASSNAME;
}
/*
* Standard default factory for the class.
*
*/
public static StandardListenService newStandardlistenService()
throws WTException {
StandardListenService instance = new StandardListenService();
instance. initialize ();
return instance;
}
/*
* Initializations of event listeners
*/
/* Override inherited method */
protected void performStartupProcess() throws ManagerException {
/* Standard debug output */
if (DEBUG && DebugProperties.isTrace (this))
LOG.enter(CLASSNAME, "performStartupProcess”) ;
/* Standard way to become admin */
SessionContext prev = SessionContext.newContext ();
try{
SessionHelper.manager.setidministrator ();
}
catch (WTException wte) {
System.err.println("StandardListenService: failed to set Administrator (ok
if installation)");
return;
}
finally {
SessionContext.setContext (prev) ;
}

/* Change: Add event listeners here */
getManagerService().addEventListener(
new ServiceEventListenerAdapter(this.getClass().getName()) {
@Override
public void notifyVetoableMultiObjectEvent(Object event) throws WTException {
PersistenceManagerEvent pmEvent =(PersistenceManagerEvent)event;
WTCollection targets = (WTCollection)pmEvent.getEventTarget();
WTCollection mdEntityList = targets.subCollection(MDEntityDemo.class, true);
if(mdEntityList.size() > 0)
{
String errorMessage = "People or Place Objects can not be deleted";
throw new WTException(errorMessage);
}
}
}, PersistenceManagerEvent.generateEventKey(
PersistenceManagerEvent.class.getName(), PersistenceManagerEvent.PRE_DELETE));
/* Standard debug output */
if (DEBUG && DebugProperties.isTrace (this))
LOG.exit(CLASSNAME, "performStartupProcess”) ;
}
}
これは役に立ちましたか?