1 22 23 package org.netbeans.lib.jmi.uml2mof.servlet; 24 25 import java.util.*; 26 import java.io.*; 27 import javax.servlet.*; 28 import javax.servlet.http.*; 29 import org.apache.commons.fileupload.*; 30 import java.util.Iterator ; 31 import javax.jmi.model.ModelPackage; 32 import javax.jmi.model.MofPackage; 33 import javax.jmi.xmi.XmiReader; 34 import javax.jmi.xmi.XmiWriter; 35 36 import org.netbeans.api.mdr.CreationFailedException; 37 import org.netbeans.api.mdr.MDRManager; 38 import org.netbeans.api.mdr.MDRepository; 39 import org.netbeans.lib.jmi.uml2mof.Transformer; 40 import org.omg.uml.UmlPackage; 41 import org.openide.ErrorManager; 42 import org.openide.util.Lookup; 43 44 public class TransformServlet extends HttpServlet { 45 46 private static final String MOF_INSTANCE = "MOFInstance"; 47 private static final String UML_INSTANCE = "UMLInstance"; 48 private static final String UML_MM = "UML"; 49 private static final String TRANSFORM_IN_PROGRESS = "<meta http-equiv=\"refresh\" content=\"0;url='TransformServlet'\"/><html><body><font size=\"medium\">Please wait! Transforming UML 1.4 to MOF 1.4...</font></body></html>"; 50 private static final String NOTHING_TO_TRANSFORM = "There is no file on the session to transform"; 51 52 private String fileStore; 53 private FileUpload fu; 54 55 class TransformInfo { 56 TransformInfo(FileItem fi) { this.fi = fi; } 57 public FileItem getFileItem() { return fi; } 58 private FileItem fi; 59 } 60 61 public void init(ServletConfig config) throws ServletException { 62 super.init(config); 63 try { 64 fu = new FileUpload(); 65 Integer maxFileSize = new Integer (config.getInitParameter("maxFileSize")); 67 fu.setSizeMax(maxFileSize.intValue()); 68 Integer maxMemSize = new Integer (config.getInitParameter("maxMemSize")); 70 fu.setSizeThreshold(maxMemSize.intValue()); 71 fileStore = config.getInitParameter("fileStore"); 73 fu.setRepositoryPath(fileStore); 74 System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName", "org.netbeans.mdr.persistence.memoryimpl.StorageFactoryImpl"); 76 } catch (Exception cfe) { 77 throw new ServletException(cfe); 78 } 79 } 80 81 private MofPackage getUmlPackage(MDRepository rep) throws Exception { 82 ModelPackage umlMM = (ModelPackage) rep.getExtent(UML_MM); 83 if (umlMM == null) { 84 umlMM = (ModelPackage) rep.createExtent(UML_MM); 85 } 86 MofPackage result = getUmlPackage(umlMM); 87 if (result == null) { 88 XmiReader reader = (XmiReader)Lookup.getDefault().lookup(XmiReader.class); 89 reader.read(UmlPackage.class.getResource("resources/01-02-15_Diff.xml").toString(), umlMM); 90 } 91 result = getUmlPackage(umlMM); 92 return result; 93 } 94 95 private MofPackage getUmlPackage(ModelPackage umlMM) { 96 for (Iterator it = umlMM.getMofPackage().refAllOfClass().iterator(); it.hasNext();) { 97 MofPackage pkg = (MofPackage) it.next(); 98 if (pkg.getContainer() == null && "UML".equals(pkg.getName())) { 99 return pkg; 100 } 101 } 102 return null; 103 } 104 105 private String formatException(Exception e) { 106 StringWriter sw = new StringWriter(); 107 PrintWriter pw = new PrintWriter(sw); 108 e.printStackTrace(pw); 109 return e.toString() + "\n" + sw.toString(); 110 } 111 112 private void doTransform(FileItem fi, OutputStream out) throws Exception { 113 XmiReader reader = (XmiReader)Lookup.getDefault().lookup(XmiReader.class); 114 XmiWriter writer = (XmiWriter)Lookup.getDefault().lookup(XmiWriter.class); 115 MDRepository rep = MDRManager.getDefault().getDefaultRepository(); 116 rep.beginTrans(true); 117 try { 118 ModelPackage mof = (ModelPackage) rep.getExtent(MOF_INSTANCE); 119 UmlPackage uml = (UmlPackage) rep.getExtent(UML_INSTANCE); 120 if (mof == null) { 121 mof = (ModelPackage)rep.createExtent(MOF_INSTANCE); 122 } 123 if (uml == null) { 124 uml = (UmlPackage)rep.createExtent(UML_INSTANCE, getUmlPackage(rep)); 125 } 126 reader.read(fi.getInputStream(), "", uml); 127 Transformer.execute(uml, mof); 128 writer.write(out, mof, null); 129 } finally { 130 rep.endTrans(true); 131 } 132 } 133 134 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException 135 { 136 try { 137 HttpSession session = req.getSession(); 138 TransformInfo ti = (TransformInfo)session.getAttribute("transform"); 139 if (ti != null) { 140 try { 141 FileItem fi = ti.getFileItem(); 142 doTransform(fi, res.getOutputStream()); 143 res.setContentType(fi.getContentType()); 144 res.setHeader("Content-Disposition", "inline;filename=" + fi.getName()); 145 } finally { 146 session.setAttribute("transform", null); 147 } 148 } else { 149 res.getWriter().println(NOTHING_TO_TRANSFORM); 150 } 151 } catch (Exception e) { 152 throw new ServletException(e); 153 } 154 } 155 156 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException 157 { 158 try { 159 HttpSession session = req.getSession(); 160 TransformInfo ti = (TransformInfo)session.getAttribute("transform"); 161 if (ti == null) { 162 List fileItems = fu.parseRequest(req); 163 Iterator i = fileItems.iterator(); 164 String redirect = null; 165 while (i.hasNext()) { 166 FileItem fi = (FileItem)i.next(); 167 if ("umlfile".equals(fi.getFieldName()) && ti == null) { 168 ti = new TransformInfo(fi); 169 session.setAttribute("transform", ti); 170 } 171 if ("redirect".equals(fi.getFieldName()) && redirect == null) 172 redirect = fi.getString(); 173 } 174 if (redirect != null) 175 res.sendRedirect(res.encodeRedirectURL(redirect)); 176 } else { 177 PrintWriter out = res.getWriter(); 178 out.println(TRANSFORM_IN_PROGRESS); 179 } 180 } catch (Exception e) { 181 try { 182 (res.getWriter()).println(formatException(e)); 183 } catch (Exception n) { 184 throw new ServletException(n); 185 } 186 } 187 } 188 } 189 | Popular Tags |