001: package com.bostechcorp.cbesb.common.util.macro;
002:
003: import java.io.File;
004: import java.io.FileFilter;
005: import java.io.FileInputStream;
006: import java.io.InputStream;
007: import java.util.Vector;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.eclipse.ui.IEditorPart;
012:
013: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
014: import com.bostechcorp.cbesb.common.util.project.ProjectRuntimeUtil;
015: import com.bostechcorp.cbesb.common.util.project.ProjectUtil;
016:
017: public class MacroUtil {
018: public static String BUILTIN_CBESB_HOME = "cbesb_home";
019: public static String BUILTIN_CBESB_IDEWS = "cbesb_idews";
020: public static String BUILTIN_CBESB_JBI_SA_PROJ = "cbesb_jbi_sa_proj";
021: public static String BUILTIN_CBESB_ESB_PROJ = "cbesb_esb_proj";
022:
023: //private static boolean init=false;
024: private static long lastModifiedTime = 0;
025: protected final static Log logger = LogFactory
026: .getLog(MacroUtil.class);
027:
028: public static String getWholeMarco(String data) {
029: return "${" + data + "}";
030: }
031:
032: public static String resolveBuiltinMacro(String data,
033: String projName) {
034: IMacroResolver resolver = new BuiltinMacroResolverImpl(projName);
035: return resolveMacro(data, resolver);
036:
037: }
038:
039: public static String resolveBuiltinMacro(String data,
040: IEditorPart editor) {
041: IMacroResolver resolver = new BuiltinMacroResolverImpl(
042: ProjectUtil.getProjectName(editor));
043: return resolveMacro(data, resolver);
044:
045: }
046:
047: private static String resolveMacro(String data,
048: IMacroResolver resolver) {
049: if (data == null)
050: return null;
051: StringBuffer buf = new StringBuffer(data);
052: MacroScanner scanner = new MacroScanner(data);
053: Vector<String> list = scanner.getMacroList();
054: for (String in : list) {
055: String result = resolver.resolveMacro(in, data);
056: if (result == null)
057: continue;
058: String macro = getWholeMarco(in);
059: int pos = buf.indexOf(macro);
060: buf = new StringBuffer(buf.substring(0, pos))
061: .append(result).append(
062: buf.substring(pos + macro.length()));
063: }
064: return buf.toString();
065: }
066:
067: public static String changeToMacroPath(String path, String projName) {
068:
069: if (path.indexOf("::") == -1)
070: return path;
071: String[] arr = path.split("::");
072: if (arr.length > 2) {
073: String esbName = ProjectRuntimeUtil
074: .getDependentESBProjName(projName);
075: if (path.startsWith(projName + "::" + esbName + "::")) {
076: path = getWholeMarco(BUILTIN_CBESB_ESB_PROJ)
077: + path.substring(path.lastIndexOf("::"));
078: }
079: } else {
080: if (ProjectRuntimeUtil.isESBProj(projName))
081: path = getWholeMarco(BUILTIN_CBESB_ESB_PROJ)
082: + path.substring(path.indexOf("::"));
083:
084: else
085: path = getWholeMarco(BUILTIN_CBESB_JBI_SA_PROJ)
086: + path.substring(path.indexOf("::"));
087:
088: }
089: return path;
090:
091: }
092:
093: public static String resolveUserMacro(String data) {
094:
095: try {
096: loadMacro();
097: } catch (Exception e) {
098: e.printStackTrace();
099: return data;
100: }
101:
102: IMacroResolver resolver = new UserMacroResolverImpl();
103: return resolveMacro(data, resolver);
104:
105: }
106:
107: // public static void reLoadMacro() throws Exception{
108: //
109: // loadMacroFile();
110: // init=true;
111: //
112: // }
113:
114: private static long getLastModifiedTime() throws Exception {
115: File dir = new File(EsbPathHelper.getCbesbHomeDir()
116: + "/config/macros");//$NON-NLS-1$ //$NON-NLS-2$
117: return dir.lastModified();
118: }
119:
120: private static void loadMacroFile() throws Exception {
121:
122: clearMacro();
123:
124: logger.debug("read macro from file");
125: IMacroResolver resolver = new UserMacroResolverImpl();
126:
127: File dir = new File(EsbPathHelper.getCbesbHomeDir()
128: + "/config/macros");//$NON-NLS-1$ //$NON-NLS-2$
129:
130: // Lists every modules in the lib dir
131: File[] files = dir.listFiles(new FileFilter() {
132: public boolean accept(File file) {
133: return (file.toString().endsWith(".macro"));//$NON-NLS-1$
134: }
135: });
136:
137: if (files != null) {
138: for (int i = 0; i < files.length; i++) {
139: File file = files[i];
140: InputStream input = new FileInputStream(file);
141: resolver.loadMacro(input);
142: input.close();
143:
144: }
145: }
146:
147: }
148:
149: private static void loadMacro() throws Exception {
150:
151: if (lastModifiedTime == 0) {
152:
153: loadMacroFile();
154: } else {
155: long time = getLastModifiedTime();
156: if (time != lastModifiedTime) {
157: loadMacroFile();
158: lastModifiedTime = time;
159: }
160: }
161: // init=true;
162:
163: }
164:
165: private static void clearMacro() {
166:
167: IMacroResolver resolver = new UserMacroResolverImpl();
168: resolver.clearMacro();
169: // init=false;
170: }
171: }
|