一、擴展服務實例
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;
}
}