SuperMap iServer Extension Service and extension service provider example, supermapiserver
1. extended service instances
1. Compress listener. java under the iserver-extend1 into a jar package
2. Copy to D: \ SuperMap-iServer \ webapps \ iserver \ WEB-INF \ lib
3. Add the listener Configuration in the web. xml file as follows:
<Listener>
<Listener-class> extend. listener </listener-class>
</Listener>
Sample Code:
Package extend;
Import javax. servlet. ServletContext;
Import javax. servlet. ServletContextEvent;
Import javax. servlet. ServletContextListener;
Import com. supermap. data. Workspace;
Import com. supermap. data. WorkspaceConnectionInfo;
Import com. supermap. data. WorkspaceType;
Public class listener implements ServletContextListener {
Private Workspace m_workspace;
// Define the destroy Function
Public void contextDestroyed (ServletContextEvent sce ){
// TODO Auto-generated method stub
System. out. println ("this is last destroyeed ");
}
// Defines the initialization function. When tomcat is started, it triggers the defined method for saving the oracle Workspace.
Public void contextInitialized (ServletContextEvent sce ){
// TODO Auto-generated method stub
/*
* When the Servlet container starts or terminates a Web application, the ServletContextEvent event is triggered to obtain
* Life Cycle of the ServletContext object,
*/
ServletContext sct = sce. getServletContext ();
Try {
M_workspace = new Workspace ();
// Initialize the workspace connection information class
WorkspaceConnectionInfo connectionInfo = new WorkspaceConnectionInfo ();
// Define the open workspace type as oracle
ConnectionInfo. setType (WorkspaceType. ORACLE );
// Define the connection information to the oracle database
ConnectionInfo. setServer ("ORCL ");
ConnectionInfo. setName ("x 'x ");
ConnectionInfo. setUser ("x 'x ");
ConnectionInfo. setPassword ("x 'x ");
Boolean openResult = m_workspace.open (connectionInfo );
If (openResult ){
System. out. print ("workspace opened successfully! ");
// Save the Workspace
Boolean saveResult = m_workspace.save ();
If (saveResult ){
System. out. print ("the workspace is saved successfully! ");
}
} Else {
System. out. print ("opening the workspace failed! ");
}
// Release resources
ConnectionInfo. dispose ();
M_workspace.dispose ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Ii. Extended Service Provider instances
Package com. supermap. serviceprovider;
Import com. supermap. data. Workspace;
Import com. supermap. data. WorkspaceConnectionInfo;
Import com. supermap. data. WorkspaceType;
Import com. supermap. services. components. spi. ProviderContext;
Import com. supermap. services. components. spi. ProviderContextAware;
// ProviderContextAware is used to obtain the context of the service provider. The context of the service provider can be used to obtain the configuration information of the service provider.
Public class SaveWorkspaceProvider implements ProviderContextAware, SaveWorkspace {
Private Workspace m_workspace;
// SetProviderContext is set to run during iserver startup,
Public void setProviderContext (ProviderContext context ){
ClearSetting clearSetting = (ClearSetting) context. getConfig (ClearSetting. class );
If (clearSetting = null ){
System. out. print ("the dataset configuration class cannot be cleared! ");
Return;
}
// Define the obtained workspace path
String workspacePath = (clearSetting). getWorkSpacePath ();
Try {
M_workspace = new Workspace ();
// Initialize the workspace connection information class
WorkspaceConnectionInfo connectionInfo = new WorkspaceConnectionInfo ();
ConnectionInfo. setType (WorkspaceType. SXWU );
ConnectionInfo. setServer (workspacePath );
Boolean openResult = m_workspace.open (connectionInfo );
If (openResult ){
System. out. print ("workspace opened successfully! ");
// Save the Workspace
Boolean saveResult = m_workspace.save ();
If (saveResult ){
System. out. print ("the workspace is saved successfully! ");
}
} Else {
System. out. print ("opening the workspace failed! ");
}
// Release resources
ConnectionInfo. dispose ();
M_workspace.dispose ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Package com. supermap. serviceprovider;
Public class ClearSetting {
// The workspacePath field is spelled incorrectly !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Private String workspacePath; // file workspace path
Public void setWorkspacePath (String workspacePath ){
This. workspacePath = workspacePath;
}
Public String getWorkSpacePath (){
// TODO Auto-generated method stub
Return workspacePath;
}
}