1 29 30 package com.caucho.j2ee.deployserver; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.RawString; 34 import com.caucho.util.L10N; 35 import com.caucho.vfs.Vfs; 36 import com.caucho.vfs.WriteStream; 37 import com.caucho.xml.XmlPrinter; 38 39 import org.w3c.dom.Node ; 40 41 import javax.annotation.PostConstruct; 42 import java.io.IOException ; 43 import java.io.OutputStream ; 44 import java.util.ArrayList ; 45 46 49 public class DeploymentPlan { 50 private static final L10N L = new L10N(DeploymentPlan.class); 51 52 private String _archiveType; 53 private String _name; 54 private String _metaInf; 55 56 private ArrayList <PlanFile> _fileList = new ArrayList <PlanFile>(); 57 58 61 public void setArchiveType(String type) 62 throws ConfigException 63 { 64 if (type.equals("war")) { 65 _metaInf = "WEB-INF/"; 66 } 67 else if (type.equals("ear")) { 68 _metaInf = "META-INF/"; 69 } 70 else if (type.equals("rar")) { 71 _metaInf = "META-INF/"; 72 } 73 else 74 throw new ConfigException(L.l("'{0}' is an unknown archive type.", type)); 75 76 _archiveType = type; 77 } 78 79 82 public String getArchiveType() 83 { 84 return _archiveType; 85 } 86 87 90 public void setName(String name) 91 { 92 _name = name; 93 } 94 95 98 public String getName() 99 { 100 return _name; 101 } 102 103 @PostConstruct 104 public void init() 105 { 106 if (_archiveType == null) 107 throw new ConfigException(L.l("`{0}' is required", "archive-type")); 108 109 if (_name == null) 110 throw new ConfigException(L.l("`{0}' is required", "name")); 111 } 112 113 117 public ExtFile createExtFile() 118 { 119 return new ExtFile(); 120 } 121 122 public void addExtFile(ExtFile extFile) 123 { 124 _fileList.add(extFile); 125 } 126 127 128 132 public RawFile createRawFile() 133 { 134 return new RawFile(); 135 } 136 137 public void addRawFile(RawFile rawFile) 138 { 139 _fileList.add(rawFile); 140 } 141 142 145 public ArrayList <PlanFile> getFileList() 146 { 147 return _fileList; 148 } 149 150 abstract public class PlanFile { 151 abstract public String getPath(); 152 abstract public void writeToStream(OutputStream os) 153 throws IOException ; 154 public String toString() 155 { 156 return "DeploymentPlan$" + getClass().getSimpleName() + "[" + getPath() + "]"; 157 } 158 159 } 160 161 public class ExtFile 162 extends PlanFile 163 { 164 private String _name; 165 private Node _data; 166 167 170 public void setName(String name) 171 { 172 if (name.startsWith("/")) 173 throw new ConfigException(L.l("name `{0}' cannot start with /", name)); 174 175 _name = name; 176 } 177 178 public void setData(Node data) 179 { 180 _data = data.getFirstChild(); 181 } 182 183 @PostConstruct 184 public void init() 185 { 186 if (_name == null) 187 throw new ConfigException(L.l("`{0}' is required", "name")); 188 189 if (_data == null) 190 throw new ConfigException(L.l("`{0}' is required", "data")); 191 } 192 193 public String getPath() 194 { 195 return _metaInf + _name; 196 } 197 198 public void writeToStream(OutputStream os) 199 throws IOException 200 { 201 XmlPrinter xmlPrinter = new XmlPrinter(os); 202 xmlPrinter.setPretty(true); 203 xmlPrinter.printXml(_data); 204 } 205 } 206 207 public class RawFile 208 extends PlanFile 209 { 210 private String _path; 211 private String _data; 212 213 216 public void setPath(String path) 217 { 218 if (path.startsWith("/")) 219 throw new ConfigException(L.l("path `{0}' cannot start with /", path)); 220 221 _path = path; 222 } 223 224 public void setData(RawString data) 225 { 226 _data = data.getValue(); 227 } 228 229 @PostConstruct 230 public void init() 231 { 232 if (_path == null) 233 throw new ConfigException(L.l("`{0}' is required", "path")); 234 235 if (_data == null) 236 throw new ConfigException(L.l("`{0}' is required", "data")); 237 } 238 239 public String getPath() 240 { 241 return _path; 242 } 243 244 public void writeToStream(OutputStream os) 245 throws IOException 246 { 247 WriteStream writeStream = Vfs.openWrite(os); 248 249 try { 250 writeStream.print(_data); 251 } 252 finally { 253 writeStream.close(); 254 } 255 } 256 } 257 } 258 259 | Popular Tags |