一、扩展服务实例
1.将iserver-extend1下的listener.java打成jar包
2.复制到D:\SuperMap-iServer\webapps\iserver\WEB-INF\lib
3.在web.xml文件中添加listener配置如下:
<listener>
<listener-class>extend.listener</listener-class>
</listener>
示例代码:
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;
// 定义的销毁函数
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("this is last destroyeed");
}
// 定义的初始化函数,当tomcat启动时触发定义的保存oracle工作空间的方法
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
/*
* 当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件, 获取tomcat的
* ServletContext 对象的生命周期,
*/
ServletContext sct = sce.getServletContext();
try {
m_workspace = new Workspace();
// 初始化工作空间连接信息类
WorkspaceConnectionInfo connectionInfo = new WorkspaceConnectionInfo();
// 定义打开的工作空间类型为oracle
connectionInfo.setType(WorkspaceType.ORACLE);
// 定义的连接oracle数据库信息
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("打开工作空间成功!");
// 保存工作空间
boolean saveResult =m_workspace.save();
if(saveResult){
System.out.print("保存工作空间成功!");
}
} else {
System.out.print("打开工作空间失败!");
}
// 释放资源
connectionInfo.dispose();
m_workspace.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、扩展服务提供者实例
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 用于获取服务提供者上下文,通过服务提供者上下文,可以获取该服务提供者对应的配置信息
public class SaveWorkspaceProvider implements ProviderContextAware, SaveWorkspace {
private Workspace m_workspace;
// setProviderContext设置在iserver启动过程中执行,
public void setProviderContext(ProviderContext context) {
ClearSetting clearSetting = (ClearSetting) context.getConfig(ClearSetting.class);
if (clearSetting == null) {
System.out.print("无法清除数据集配置类!");
return;
}
// 定义的获取到的工作空间路径
String workspacePath = (clearSetting).getWorkSpacePath();
try {
m_workspace = new Workspace();
// 初始化工作空间连接信息类
WorkspaceConnectionInfo connectionInfo = new WorkspaceConnectionInfo();
connectionInfo.setType(WorkspaceType.SXWU);
connectionInfo.setServer(workspacePath);
boolean openResult = m_workspace.open(connectionInfo);
if (openResult) {
System.out.print("打开工作空间成功!");
// 保存工作空间
boolean saveResult = m_workspace.save();
if (saveResult) {
System.out.print("保存工作空间成功!");
}
} else {
System.out.print("打开工作空间失败!");
}
// 释放资源
connectionInfo.dispose();
m_workspace.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.supermap.serviceprovider;
public class ClearSetting {
//workspacePath 字段拼错了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
private String workspacePath; //文件工作空间路径
public void setWorkspacePath(String workspacePath) {
this.workspacePath = workspacePath;
}
public String getWorkSpacePath() {
// TODO Auto-generated method stub
return workspacePath;
}
}