View Javadoc
1   package gboat2.cxf.action;
2   
3   import gboat2.base.core.annotation.Business;
4   import gboat2.base.core.annotation.Domain;
5   import gboat2.base.core.annotation.Module;
6   import gboat2.base.core.annotation.Operation;
7   import gboat2.base.core.annotation.Operations;
8   import gboat2.base.core.annotation.Preference;
9   import gboat2.base.core.dao.Page;
10  import gboat2.base.core.web.BaseActionSupport;
11  import gboat2.base.core.web.JsonResultSupport;
12  import gboat2.cxf.Constant;
13  import gboat2.cxf.business.IWebServiceConfigBusiness;
14  import gboat2.cxf.model.WebServiceConfig;
15  import gboat2.cxf.model.WebServiceConfigParam;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  import java.util.Map;
20  
21  import net.sf.json.JSONArray;
22  import net.sf.json.JSONObject;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.struts2.convention.annotation.ParentPackage;
26  import org.apache.struts2.convention.annotation.ResultPath;
27  import org.apache.struts2.convention.annotation.Results;
28  import org.osgi.framework.Bundle;
29  import org.osgi.framework.BundleContext;
30  import org.osgi.framework.Constants;
31  import org.osgi.framework.ServiceReference;
32  import org.osgi.service.startlevel.StartLevel;
33  import org.springframework.beans.factory.annotation.Autowired;
34  
35  @ParentPackage("webservice")
36  @Domain(WebServiceConfig.class)
37  @ResultPath("/content/webServiceConfig")
38  @Results( { @org.apache.struts2.convention.annotation.Result(name = "return-list", location = "/web-service-config!list.do", type = "redirect") })
39  @Module(name = "web服务配置", desc = "用来增删查改web服务,以及发布、取消发布web服务")
40  @Operations(value = { @Operation(name = "添加web服务", code = "add"), @Operation(name = "编辑web服务", code = "edit"),
41          @Operation(name = "删除web服务", code = "delete") })
42  @Business(IWebServiceConfigBusiness.class)
43  @Preference("系统配置/web 服务管理")
44  public class WebServiceConfigAction extends BaseActionSupport {
45  	
46  	/** */
47  	private static final long serialVersionUID = 1L;
48  	
49  	private transient WebServiceConfig webSerConfig;
50  	
51  	private String bundleName;
52  	
53  	private List<String> bundleNames = new ArrayList<String>();
54  	
55  	private List<String> interfaceName = new ArrayList<String>();
56  	
57  	private boolean isEdit = false;
58  	
59  	public List<String> getInterfaceName() {
60  		return interfaceName;
61  	}
62  	
63  	public void setInterfaceName(List<String> interfaceName) {
64  		this.interfaceName = interfaceName;
65  	}
66  	
67  	@Autowired
68  	private transient IWebServiceConfigBusiness webSer;
69  	
70  	@Autowired
71  	private transient StartLevel startLevelSer;
72  	
73  	protected void initList(Map<String, Object> params) {
74  	}
75  	
76  	@Override
77  	protected void postList(Page pageinfo) {
78  		List<WebServiceConfig> webServiceConfigList = pageinfo.getResult();
79  		for (WebServiceConfig cfg : (List<WebServiceConfig>) webServiceConfigList) {
80  			cfg.setRelativeAddress(getHostAddress() + cfg.getRelativeAddress() + "?wsdl");
81  		}
82  	}
83  	
84  	private String getHostAddress() {
85  		WebServiceConfigParam siteAddress = webSer.getWebSerConfParam(IWebServiceConfigBusiness.SITE_ADDRESS_KEY);
86  		WebServiceConfigParam port = webSer.getWebSerConfParam(IWebServiceConfigBusiness.PORT_KEY);
87  		return siteAddress.getParamValue() + ":" + port.getParamValue() + "/";
88  	}
89  	
90  	@Override
91  	protected void initEdit() {
92  		Bundle[] bundles = context.getBundles();
93  		for (Bundle tempBundle : bundles) {
94  			//只有第二级以上的Bundle被获取
95  			if (startLevelSer.getBundleStartLevel(tempBundle) >= 3) {
96  				bundleNames.add(tempBundle.getSymbolicName());
97  				
98  			}
99  			
100 		}
101 		webSerConfig = (WebServiceConfig) getModel();
102 		if (StringUtils.isEmpty(webSerConfig.getConfigId())) {
103 			isEdit = false;
104 		} else {
105 			isEdit = true;
106 		}
107 		
108 		Bundle bundle = getBundleBySymbolicName(context, webSerConfig.getBundleName());
109 		if (null != bundle) {
110 			String exportPckNames = (String) bundle.getHeaders().get(Constants.EXPORT_PACKAGE);
111 			ServiceReference[] srs = bundle.getRegisteredServices();
112 			if (null != srs) {
113 				for (ServiceReference sr : srs) {
114 					String[] names = (String[]) sr.getProperty(Constants.OBJECTCLASS);
115 					if (null == names || names.length == 0) {
116 						continue;
117 					}
118 					String serviceName = names[0];
119 					
120 					//去掉中括号
121 					//String deleteFront = sr.toString().substring(1);
122 					//String serviceName = deleteFront.substring(0, deleteFront.length() - 1);
123 					if (webSer.decideCurrentService(exportPckNames, serviceName)) {
124 						interfaceName.add(serviceName);
125 					}
126 				}
127 			}
128 		}
129 		
130 	}
131 	
132 	protected void postSave() {
133 		WebServiceConfig webSerConfig = (WebServiceConfig) getModel();
134 		
135 		if (Constant.WEBSERVICE_STATUS_ACTIVE.equals(webSerConfig.getStatus())) {
136 			webSer.publishService(webSerConfig);
137 		} else {
138 			webSer.unpublishService(webSerConfig);
139 		}
140 	}
141 	
142 	//激活web服务
143 	public String activeWebservice() {
144 		WebServiceConfig webSerConfig = (WebServiceConfig) getModel();
145 		
146 		ServiceReference serRef = this.context.getServiceReference(webSerConfig.getInterfaceName());
147 		if (null != serRef) {
148 			webSer.publishService(webSerConfig);
149 		} else {
150 			webSerConfig.setStatus("0");
151 			webSer.update(webSerConfig);
152 		}
153 		return "return-list";
154 	}
155 	
156 	//取消激活web服务
157 	public String inactiveWebservice() {
158 		WebServiceConfig webSerConfig = (WebServiceConfig) getModel();
159 		webSer.unpublishService(webSerConfig);
160 		//		webSerConfig.setStatus("0");//重构到bussiness中
161 		//		webSer.update(webSerConfig);
162 		return "return-list";
163 	}
164 	
165 	public void getInterfaces() {
166 		JSONArray returnJSONArray = new JSONArray();
167 		if (!StringUtils.isEmpty(bundleName)) {
168 			
169 			Bundle bundle = getBundleBySymbolicName(context, bundleName);
170 			if (null != bundle) {
171 				String exportPckNames = (String) bundle.getHeaders().get(Constants.EXPORT_PACKAGE);
172 				ServiceReference[] srs = bundle.getRegisteredServices();
173 				if (null != srs) {
174 					for (ServiceReference sr : srs) {
175 						String[] names = (String[]) sr.getProperty(Constants.OBJECTCLASS);
176 						if (null == names || names.length == 0) {
177 							continue;
178 						}
179 						String serviceName = names[0];
180 						//去掉中括号
181 						//String deleteFront = sr.toString().substring(1);
182 						//String deleteTail = deleteFront.substring(0, deleteFront.length() - 1);
183 						if (webSer.decideCurrentService(exportPckNames, serviceName)) {
184 							//TODO:采用toString似乎不够稳定
185 							JSONObject item = new JSONObject();
186 							item.put("key", serviceName);
187 							item.put("value", serviceName);
188 							returnJSONArray.add(item);
189 						}
190 					}
191 				}
192 			}
193 		}
194 		JsonResultSupport.output(JSONArray.fromObject(returnJSONArray), response);
195 	}
196 	
197 	private Bundle getBundleBySymbolicName(BundleContext bundleContext, String bundleName) {
198 		Bundle[] bundles = bundleContext.getBundles();
199 		for (Bundle bundle : bundles) {
200 			if (bundle.getSymbolicName().equals(bundleName)) {
201 				return bundle;
202 				
203 			}
204 		}
205 		return null;
206 	}
207 	
208 	public WebServiceConfig getWebSerConfig() {
209 		return webSerConfig;
210 	}
211 	
212 	public void setWebSerConfig(WebServiceConfig webSerConfig) {
213 		this.webSerConfig = webSerConfig;
214 	}
215 	
216 	public List<String> getBundleNames() {
217 		return bundleNames;
218 	}
219 	
220 	public void setBundleNames(List<String> bundleNames) {
221 		this.bundleNames = bundleNames;
222 	}
223 	
224 	public String getBundleName() {
225 		return bundleName;
226 	}
227 	
228 	public void setBundleName(String bundleName) {
229 		this.bundleName = bundleName;
230 	}
231 
232 	
233     public boolean getIsEdit() {
234     	return isEdit;
235     }
236 
237 	
238     public void setIsEdit(boolean isEdit) {
239     	this.isEdit = isEdit;
240     }
241 	
242 }