001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.common.util;
025:
026: import java.io.File;
027: import java.io.IOException;
028:
029: import org.apache.commons.lang.StringUtils;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: public class EsbPathHelper {
034: private static final Log log = LogFactory
035: .getLog(EsbPathHelper.class);
036:
037: protected static final String CBESB_HOME = "CBESB_HOME";
038:
039: protected static final String CBESB_HOME_SYSPROP = "cbesb.home";
040:
041: protected static String CBESB_UI_WS = "CBESB_UI_WS";
042:
043: protected static String CBESB_UI_WS_SYSPROP = "cbesb.ui.ws";
044:
045: protected static String CBESB_SERVER_MODE = "CBESB_SERVER_MODE";
046:
047: protected static final String CBESB_SERVER_MODE_SYSPROP = "cbesb.server.mode";
048:
049: protected static String basePackageName = "com.bostechcorp.cbesb.fsmparser";
050:
051: protected static String mdlFolder = "src/formats/";
052:
053: //alert monitor values
054: protected static String ALERT_HOME = "ALERT_HOME";
055: protected static String activatedAlertFolder = "activated";
056:
057: public static String getCbesbUiWorkSpace() {
058: String esbHome = getCbesbHomeDir();
059:
060: String uiWorkspace = System.getenv(CBESB_UI_WS);
061: String uiWorkspaceProp = System
062: .getProperty(CBESB_UI_WS_SYSPROP);
063: if (uiWorkspaceProp != null)
064: uiWorkspace = uiWorkspaceProp;
065:
066: if (uiWorkspace == null && esbHome != null) {
067: uiWorkspace = esbHome + File.separator + "ideworkspace";
068: }
069: File file = new File(uiWorkspace);
070: try {
071: return file.getCanonicalPath();
072: } catch (IOException e) {
073: log.error("get cbesb workspace dir error:", e);
074: }
075:
076: return uiWorkspace;
077:
078: }
079:
080: public static String getCbesbHomeDir() {
081: String esbHome = System.getenv(CBESB_HOME);
082: String esbHomeProp = System.getProperty(CBESB_HOME_SYSPROP);
083:
084: // Always to use the -D option to overwrite the system enviroment
085: if (esbHomeProp != null)
086: esbHome = esbHomeProp;
087: File file = new File(esbHome);
088: try {
089: return file.getCanonicalPath();
090: } catch (IOException e) {
091: log.error("get cbesb home dir error:", e);
092: }
093:
094: return esbHome;
095: }
096:
097: public static String getFullPathForDef(String defStr) {
098:
099: if (defStr.indexOf("::") == -1) {
100: // this is for Junit test case, read directly from local file system
101: return defStr;
102: } else {
103:
104: // defStr=MacroUtil.replaceBuiltinMacro(defStr);
105:
106: int index = defStr.lastIndexOf("::");
107: String substringAfter = defStr.substring(index + 2, defStr
108: .length());
109: // the MsgDef is in the format of "projName::src/formats/file.mdl"
110:
111: if (substringAfter.indexOf(":") > -1) {
112: substringAfter = StringUtils.substringBefore(
113: substringAfter, ":");
114: }
115: String projName = defStr.substring(0, index);
116: projName = handelProjectName(projName, "/");
117:
118: String uiWorkspace = getCbesbUiWorkSpace();
119:
120: return uiWorkspace + File.separator + projName
121: + File.separator + substringAfter;
122:
123: }
124:
125: }
126:
127: /**
128: *
129: * @param msgDefStr
130: * @return Array 0--fsmClassName, 1--messageName, 2--SA name
131: * @throws Exception
132: */
133: public static String[] getFsmClassName(String msgDefStr)
134: throws Exception {
135: String fsmClass = null;
136: String messageName = null;
137: String saName = null;
138: int index = msgDefStr.lastIndexOf("::");
139: if (index > -1) {
140: String substringAfter = msgDefStr.substring(index + 2,
141: msgDefStr.length());
142: // the MsgDef is in the format of "projName::src/formats/file.mdl"
143:
144: String projName = msgDefStr.substring(0, index);
145: int firstSep = projName.indexOf("::");
146: if (firstSep >= 0)
147: saName = projName.substring(0, firstSep);
148: else
149: saName = projName;
150: projName = handelProjectName(projName, ".");
151:
152: fsmClass = basePackageName + "." + projName;
153:
154: if (substringAfter.indexOf(":") > -1) {
155: String remainStr = StringUtils.substringBefore(
156: substringAfter, ":");
157: messageName = StringUtils.substringAfter(
158: substringAfter, ":");
159:
160: String packageName = StringUtils.substringBetween(
161: substringAfter, mdlFolder, ":");
162: String[] packages = StringUtils.split(packageName, "/");
163: for (int i = 0; i < packages.length - 1; i++) {
164: fsmClass = fsmClass + "." + packages[i];
165: }
166: String mdlFileName = StringUtils.substringBefore(
167: packages[packages.length - 1], ".");
168:
169: fsmClass = fsmClass + "." + mdlFileName + "."
170: + messageName + "." + messageName;
171: }
172: }
173:
174: String[] result = new String[] { fsmClass, messageName, saName };
175: return result;
176: }
177:
178: public static String handelProjectName(String projName,
179: String delimiter) {
180: if (projName.contains("::")) {
181: if (isServerMode())
182: projName = projName.replace("::", delimiter);
183: else
184: projName = StringUtils.substringAfter(projName, "::");
185:
186: }
187: return projName;
188: }
189:
190: public static String getTrnClassName(String trnName) {
191: String fullName = trnName
192: .substring(0, trnName.lastIndexOf('.'));
193: int lastColon = fullName.lastIndexOf("::");
194: String projectSpecifier = fullName.substring(0, lastColon);
195: if (projectSpecifier.indexOf("::") >= 0)
196: projectSpecifier = projectSpecifier
197: .substring(projectSpecifier.indexOf("::") + 2);
198: String pathPart = fullName.substring(lastColon + 2);
199: pathPart = pathPart.replace("src/xlate/", "");
200: pathPart = pathPart.replace("/", ".");
201: String className = "com.bostechcorp.cbesb.generated.map."
202: + projectSpecifier + "." + pathPart;
203: return className;
204: }
205:
206: public static String getProjectName(String defName) {
207: String projectName = defName
208: .substring(0, defName.indexOf("::"));
209: return projectName;
210: }
211:
212: /**
213: *
214: * @param msgDefStr the MsgDef is in the format of "projName::v004010/variant/m270"
215: * @return Array 0--projectName
216: * 1-- x12 xersion
217: * 2--variant
218: * 3--message
219: * 4--service assembly name
220: * ProjectName: if the msgDefStr contains ESB project Name,
221: * the return projectName like JSAProjName/ESBProjName
222: */
223: public static String[] getVariantMessage(String msgDefStr) {
224: String remainStr = msgDefStr;
225: String projName = "";
226: String saName = "";
227: int firstColons = msgDefStr.indexOf("::");
228: if (firstColons > -1) {
229: saName = msgDefStr.substring(0, firstColons);
230: int index = msgDefStr.lastIndexOf("::");
231: remainStr = msgDefStr.substring(index + 2, msgDefStr
232: .length());
233: if (remainStr.indexOf(":") > -1) {
234: remainStr = StringUtils.substringBefore(remainStr, ":");
235: }
236: projName = msgDefStr.substring(0, index);
237: projName = handelProjectName(projName, "/");
238:
239: }
240:
241: String[] msgDefs = remainStr.split("/");
242: if (msgDefs[2].endsWith(".mdl")) {
243: msgDefs[2] = msgDefs[2].substring(0, msgDefs[2]
244: .lastIndexOf(".mdl"));
245: }
246: String[] result = new String[] { projName, msgDefs[0],
247: msgDefs[1], msgDefs[2], saName };
248: return result;
249:
250: }
251:
252: public static boolean isServerMode() {
253: String serverMode = System.getenv(CBESB_SERVER_MODE);
254: String sysServerMode = System
255: .getProperty(CBESB_SERVER_MODE_SYSPROP);
256: if (sysServerMode != null)
257: serverMode = sysServerMode;
258: log.debug("ESB server mode:" + serverMode);
259:
260: if (serverMode != null && serverMode.equalsIgnoreCase("true")) {
261: return true;
262: } else
263: return false;
264: }
265:
266: // public static String getAertHome()
267: // {
268: // return System.getenv(ALERT_HOME);
269: // }
270: //
271: // public static String getActivatedAlertFolder()
272: // {
273: // return getAertHome()+"/"+activatedAlertFolder;
274: // }
275:
276: }
|