KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > uml2mof > servlet > TransformServlet


1 /**
2   * The contents of this file are subject to the terms of the Common Development
3   * and Distribution License (the License). You may not use this file except in
4   * compliance with the License.
5   *
6   * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7   * or http://www.netbeans.org/cddl.txt.
8   *
9   * When distributing Covered Code, include this CDDL Header Notice in each file
10   * and include the License file at http://www.netbeans.org/cddl.txt.
11   * If applicable, add the following below the CDDL Header, with the fields
12   * enclosed by brackets [] replaced by your own identifying information:
13   * "Portions Copyrighted [year] [name of copyright owner]"
14   *
15   * The Original Software is NetBeans.
16   * The Initial Developer of the Original Software is Adaptive Inc.
17   * Portions created by Sun Microsystems, Inc. are Copyright (C) 2003 Sun
18   * Microsystems, Inc. All Rights Reserved.
19   *
20   * Contributor(s): Nick Dowler.
21   */

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 JavaDoc;
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 JavaDoc MOF_INSTANCE = "MOFInstance";
47     private static final String JavaDoc UML_INSTANCE = "UMLInstance";
48     private static final String JavaDoc UML_MM = "UML";
49     private static final String JavaDoc 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 JavaDoc NOTHING_TO_TRANSFORM = "There is no file on the session to transform";
51     
52     private String JavaDoc 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             // maximum size before a FileUploadException will be thrown
66
Integer JavaDoc maxFileSize = new Integer JavaDoc(config.getInitParameter("maxFileSize"));
67             fu.setSizeMax(maxFileSize.intValue());
68             // maximum size that will be stored in memory
69
Integer JavaDoc maxMemSize = new Integer JavaDoc(config.getInitParameter("maxMemSize"));
70             fu.setSizeThreshold(maxMemSize.intValue());
71             // the location for saving data that is larger than getSizeThreshold()
72
fileStore = config.getInitParameter("fileStore");
73             fu.setRepositoryPath(fileStore);
74             // set up mdrepository to use in memory storage
75
System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName", "org.netbeans.mdr.persistence.memoryimpl.StorageFactoryImpl");
76           } catch (Exception JavaDoc cfe) {
77             throw new ServletException(cfe);
78         }
79     }
80     
81     private MofPackage getUmlPackage(MDRepository rep) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc formatException(Exception JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc i = fileItems.iterator();
164                 String JavaDoc 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 JavaDoc e) {
181             try {
182                 (res.getWriter()).println(formatException(e));
183             } catch (Exception JavaDoc n) {
184                 throw new ServletException(n);
185             }
186         }
187      }
188 }
189
Popular Tags