PageRenderTime 69ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/jain-slee/tools/eclipslee/plugin/plugins/org.mobicents.eclipslee.servicecreation/src/org/mobicents/eclipslee/servicecreation/util/SbbFinder.java

http://mobicents.googlecode.com/
Java | 330 lines | 235 code | 34 blank | 61 comment | 18 complexity | da53fa218758a412701b7be8e723b90a MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /**
  2. * Copyright 2005 Open Cloud Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.mobicents.eclipslee.servicecreation.util;
  17. import java.io.File;
  18. import java.util.jar.JarEntry;
  19. import java.util.jar.JarFile;
  20. import org.eclipse.core.resources.IContainer;
  21. import org.eclipse.core.resources.IFile;
  22. import org.eclipse.core.resources.IFolder;
  23. import org.eclipse.core.resources.IResource;
  24. import org.eclipse.core.runtime.CoreException;
  25. import org.eclipse.core.runtime.IPath;
  26. import org.eclipse.core.runtime.Path;
  27. import org.eclipse.jdt.core.ICompilationUnit;
  28. import org.eclipse.jdt.core.JavaModelException;
  29. import org.mobicents.eclipslee.util.slee.xml.DTDXML;
  30. import org.mobicents.eclipslee.util.slee.xml.components.ComponentNotFoundException;
  31. import org.mobicents.eclipslee.util.slee.xml.components.SbbRefXML;
  32. import org.mobicents.eclipslee.util.slee.xml.components.SbbXML;
  33. import org.mobicents.eclipslee.xml.SbbJarXML;
  34. /**
  35. * @author cath
  36. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  37. */
  38. public class SbbFinder extends BaseFinder {
  39. @Override
  40. protected DTDXML loadJar(JarFile file, JarEntry entry, String jarLocation) throws Exception {
  41. return new SbbJarXML(file, entry, null);
  42. }
  43. private static SbbFinder sbbFinder = new SbbFinder();
  44. public static SbbFinder getDefault() {
  45. return sbbFinder;
  46. }
  47. protected DTDXML loadJar(JarFile jar, JarEntry entry) throws Exception {
  48. return new SbbJarXML(jar, entry, null);
  49. }
  50. protected DTDXML loadFile(IFile file) throws Exception {
  51. return new SbbJarXML(file);
  52. }
  53. protected DTDXML getInnerXML(DTDXML outerXML, String className) throws Exception {
  54. if (outerXML instanceof SbbJarXML) {
  55. SbbJarXML sbbJar = (SbbJarXML) outerXML;
  56. SbbXML sbb = sbbJar.getSbb(className);
  57. return sbb;
  58. }
  59. return null;
  60. }
  61. /**
  62. * SbbJarXML getSbbJarXML(ICompilationUnit)
  63. * IFile getSbbJarXMLFile(ICompilationUnit)
  64. *
  65. * IFile getSbbAbstractClassFile(ICompilationUnit)
  66. * IFile getSbbAbstractClassFile(IFile xmlFile, ...)
  67. *
  68. * IFile getSbbLocalObjectFile(ICompilationUnit)
  69. * IFile getSbbLocalObjectFile(IFile xmlFile, ...)
  70. *
  71. * IFile getSbbUsageInterfaceFile(ICompilationUnit)
  72. * IFile getSbbUsageInterfaceFile(IFile xmlFile, ...)
  73. *
  74. * IFile getSbbActivityContextInterfaceFile(ICompilationUnit)
  75. * IFile getSbbActivityContextInterfaceFile(IFile xmlFile, ...)
  76. */
  77. public static SbbJarXML getSbbJarXML(ICompilationUnit unit) {
  78. try {
  79. return new SbbJarXML(getSbbJarXMLFile(unit));
  80. } catch (Exception e) {
  81. return null;
  82. }
  83. }
  84. public static IFile getSbbJarXMLFile(ICompilationUnit unit) {
  85. String clazzName = EclipseUtil.getClassName(unit);
  86. try {
  87. IContainer folder = unit.getCorrespondingResource().getParent();
  88. // ammendonca: for maven we have XML at .../src/main/resources/META-INF, we are at .../src/main/java/<package>/...
  89. while(!folder.getName().equals("main")) {
  90. folder = folder.getParent();
  91. }
  92. folder = folder.getFolder(new Path("resources/META-INF"));
  93. IResource children[] = folder.members(IResource.FILE);
  94. for (int i = 0; i < children.length; i++) {
  95. // For some reason directories come in also...
  96. if(!(children[i] instanceof IFile)) {
  97. continue;
  98. }
  99. IFile file = (IFile) children[i];
  100. if (file.getName().endsWith("sbb-jar.xml")) {
  101. try {
  102. SbbJarXML xml = new SbbJarXML(file);
  103. xml.getSbb(clazzName);
  104. } catch (Exception e) {
  105. // e.printStackTrace();
  106. // System.err.println(e.getMessage());
  107. continue;
  108. }
  109. return file;
  110. }
  111. }
  112. } catch (JavaModelException e) {
  113. return null;
  114. } catch (CoreException e) {
  115. return null;
  116. }
  117. return null;
  118. }
  119. public static IFile getSbbAbstractClassFile(ICompilationUnit unit) {
  120. try {
  121. String clazzName = EclipseUtil.getClassName(unit);
  122. SbbJarXML sbbJarXML = getSbbJarXML(unit);
  123. SbbXML sbb = sbbJarXML.getSbb(clazzName);
  124. String name = sbb.getAbstractClassName();
  125. IPath path = new Path(name.replaceAll("\\.", "/") + ".java");
  126. IFolder folder = getSourceFolder(unit);
  127. IFile file = folder.getFile(path);
  128. if (file.exists()) return file;
  129. return null;
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. return null;
  133. }
  134. }
  135. public static IFile getSbbAbstractClassFile(IFile xmlFile, String name, String vendor, String version) {
  136. try {
  137. SbbJarXML sbbJarXML = new SbbJarXML(xmlFile);
  138. SbbXML sbb = sbbJarXML.getSbb(name, vendor, version);
  139. String className = sbb.getAbstractClassName();
  140. IPath path = new Path(className.replaceAll("\\.", "/") + ".java");
  141. IFolder folder = getSourceFolder(xmlFile);
  142. // ammendonca: we are at xxx/src/main/resources. move to xxx/src/main/java
  143. folder = folder.getFolder(".." + File.separator + "java");
  144. IFile file = folder.getFile(path);
  145. if (file.exists()) return file;
  146. return null;
  147. } catch (Exception e) {
  148. return null;
  149. }
  150. }
  151. public static IFile getSbbLocalObjectFile(ICompilationUnit unit) {
  152. try {
  153. String clazzName = EclipseUtil.getClassName(unit);
  154. SbbJarXML sbbJarXML = getSbbJarXML(unit);
  155. SbbXML sbb = sbbJarXML.getSbb(clazzName);
  156. String name = sbb.getLocalInterfaceName();
  157. IPath path = new Path(name.replaceAll("\\.", "/") + ".java");
  158. IFolder folder = getSourceFolder(unit);
  159. IFile file = folder.getFile(path);
  160. if (file.exists()) return file;
  161. return null;
  162. } catch (Exception e) {
  163. return null;
  164. }
  165. }
  166. public static IFile getSbbLocalObjectFile(IFile xmlFile, String name, String vendor, String version) {
  167. try {
  168. SbbJarXML sbbJarXML = new SbbJarXML(xmlFile);
  169. SbbXML sbb = sbbJarXML.getSbb(name, vendor, version);
  170. String className = sbb.getLocalInterfaceName();
  171. IPath path = new Path(className.replaceAll("\\.", "/") + ".java");
  172. IFolder folder = getSourceFolder(xmlFile);
  173. // ammendonca: we are at xxx/src/main/resources. move to xxx/src/main/java
  174. folder = folder.getFolder(".." + File.separator + "java");
  175. IFile file = folder.getFile(path);
  176. if (file.exists()) return file;
  177. return null;
  178. } catch (Exception e) {
  179. return null;
  180. }
  181. }
  182. public static IFile getSbbUsageInterfaceFile(ICompilationUnit unit) {
  183. try {
  184. String clazzName = EclipseUtil.getClassName(unit);
  185. SbbJarXML sbbJarXML = getSbbJarXML(unit);
  186. SbbXML sbb = sbbJarXML.getSbb(clazzName);
  187. String name = sbb.getUsageInterfaceName();
  188. IPath path = new Path(name.replaceAll("\\.", "/") + ".java");
  189. IFolder folder = getSourceFolder(unit);
  190. IFile file = folder.getFile(path);
  191. if (file.exists()) return file;
  192. return null;
  193. } catch (Exception e) {
  194. return null;
  195. }
  196. }
  197. public static IFile getSbbUsageInterfaceFile(IFile xmlFile, String name, String vendor, String version) {
  198. try {
  199. SbbJarXML sbbJarXML = new SbbJarXML(xmlFile);
  200. SbbXML sbb = sbbJarXML.getSbb(name, vendor, version);
  201. String className = sbb.getUsageInterfaceName();
  202. IPath path = new Path(className.replaceAll("\\.", "/") + ".java");
  203. IFolder folder = getSourceFolder(xmlFile);
  204. // ammendonca: we are at xxx/src/main/resources. move to xxx/src/main/java
  205. folder = folder.getFolder(".." + File.separator + "java");
  206. IFile file = folder.getFile(path);
  207. if (file.exists()) return file;
  208. return null;
  209. } catch (Exception e) {
  210. return null;
  211. }
  212. }
  213. public static IFile getSbbActivityContextInterfaceFile(ICompilationUnit unit) {
  214. try {
  215. String clazzName = EclipseUtil.getClassName(unit);
  216. SbbJarXML sbbJarXML = getSbbJarXML(unit);
  217. SbbXML sbb = sbbJarXML.getSbb(clazzName);
  218. String name = sbb.getActivityContextInterfaceName();
  219. IPath path = new Path(name.replaceAll("\\.", "/") + ".java");
  220. IFolder folder = getSourceFolder(unit);
  221. IFile file = folder.getFile(path);
  222. if (file.exists()) return file;
  223. return null;
  224. } catch (Exception e) {
  225. return null;
  226. }
  227. }
  228. public static IFile getSbbActivityContextInterfaceFile(IFile xmlFile, String name, String vendor, String version) {
  229. try {
  230. SbbJarXML sbbJarXML = new SbbJarXML(xmlFile);
  231. SbbXML sbb = sbbJarXML.getSbb(name, vendor, version);
  232. String className = sbb.getActivityContextInterfaceName();
  233. IPath path = new Path(className.replaceAll("\\.", "/") + ".java");
  234. IFolder folder = getSourceFolder(xmlFile);
  235. // ammendonca: we are at xxx/src/main/resources. move to xxx/src/main/java
  236. folder = folder.getFolder(".." + File.separator + "java");
  237. IFile file = folder.getFile(path);
  238. if (file.exists()) return file;
  239. return null;
  240. } catch (Exception e) {
  241. return null;
  242. }
  243. }
  244. public SbbXML getSbbXML(SbbRefXML xml, String project) {
  245. DTDXML jarXML[] = this.getComponents(project);
  246. for (int i = 0; i < jarXML.length; i++) {
  247. try {
  248. SbbXML sbb = ((SbbJarXML) jarXML[i]).getSbb(xml.getName(), xml.getVendor(), xml.getVersion());
  249. return sbb;
  250. } catch (ComponentNotFoundException e) {
  251. // Ignore, try next SBB entry.
  252. }
  253. }
  254. return null;
  255. }
  256. /**
  257. * Search the specified-sbb in the list of all SBB found in the jars directory.
  258. * @param allSbb
  259. * @param rootSbbName
  260. * @param rootSbbVendor
  261. * @param rootSbbVersion
  262. * @return the sbbXML in the list or null
  263. */
  264. public static SbbJarXML searchSBBJarXml(SbbJarXML[] allSbb, String rootSbbName, String rootSbbVendor, String rootSbbVersion) {
  265. for (int i = 0; i < allSbb.length; i++) {
  266. SbbJarXML sbbXMLJar = allSbb[i];
  267. try {
  268. sbbXMLJar.getSbb(rootSbbName, rootSbbVendor, rootSbbVersion);
  269. return sbbXMLJar;
  270. } catch (ComponentNotFoundException e) {
  271. //Skip, not found.
  272. continue;
  273. }
  274. }
  275. return null;
  276. }
  277. /**
  278. * Search the specified-sbb in the list of all SBB found in the jars directory.
  279. * @param allSbb
  280. * @param rootSbbName
  281. * @param rootSbbVendor
  282. * @param rootSbbVersion
  283. * @return the sbbXML in the list or null
  284. */
  285. public static SbbXML searchSBB(SbbXML[] allSbb, String rootSbbName, String rootSbbVendor, String rootSbbVersion) {
  286. for (int i = 0; i < allSbb.length; i++) {
  287. SbbXML sbbXML = allSbb[i];
  288. if(sbbXML.getName().equals(rootSbbName)){
  289. if(sbbXML.getVendor().equals(rootSbbVendor))
  290. if(sbbXML.getVersion().equals(rootSbbVersion))
  291. return sbbXML;
  292. }
  293. }
  294. return null;
  295. }
  296. }