private void generationFailed(Exception e, Object file) { OutputLogger.getInstance().log(e); ErrorManager.getDefault().notify(e); OutputLogger.getInstance() .log( MessageFormat.format( NbBundle.getMessage( ClientBeanGeneratorTemplate.class, "MSG_FailJavonClientGeneration"), file)); }
public boolean generateTarget(ProgressHandle ph, String target) { if (mapping.getServiceMapping(target) != null) { String msg = NbBundle.getMessage(ClientJavonTemplate.class, "MSG_Client"); // NOI18N ph.progress(msg); OutputLogger.getInstance().log(msg); mapping.setProperty("target", "client"); JavonMapping.Service service = mapping.getServiceMapping(target); FileObject outputDir = FileUtil.toFileObject( FileUtil.normalizeFile(new File(mapping.getClientMapping().getOutputDirectory()))); outputDir = outputDir.getFileObject(mapping.getClientMapping().getPackageName().replace('.', '/')); FileObject outputFile = outputDir.getFileObject(mapping.getClientMapping().getClassName(), "java"); if (outputFile == null) { OutputLogger.getInstance() .log( MessageFormat.format( NbBundle.getMessage(ClientJavonTemplate.class, "MSG_ClientJavonCreation"), mapping.getClientMapping().getClassName())); // NOI18N try { outputFile = outputDir.createData(mapping.getClientMapping().getClassName(), "java"); } catch (IOException e) { OutputLogger.getInstance() .log( LogLevel.ERROR, MessageFormat.format( NbBundle.getMessage(ClientJavonTemplate.class, "MSG_FailClientJavonCreation"), mapping.getClientMapping().getClassName())); // NOI18N } } OutputFileFormatter off = null; try { off = new OutputFileFormatter(outputFile); } catch (DataObjectNotFoundException e) { generationFailed(e, outputFile); return false; } catch (IOException e) { generationFailed(e, outputFile); return false; } ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine eng = mgr.getEngineByName("freemarker"); Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); FileObject template = FileUtil.getConfigFile("Templates/Client/Client.java"); OutputLogger.getInstance() .log(NbBundle.getMessage(ClientJavonTemplate.class, "MSG_ConfigureBindings")); // NOI18N Set<ClassData> returnTypes = service.getReturnTypes(); Set<ClassData> parameterTypes = service.getParameterTypes(); bind.put("mapping", mapping); bind.put("registry", mapping.getRegistry()); bind.put("returnTypes", returnTypes); bind.put("parameterTypes", parameterTypes); bind.put("service", service); bind.put("utils", new Utils(mapping.getRegistry())); // Compute imports for JavaBeans Set<String> imports = new HashSet<String>(); for (ClassData cd : parameterTypes) { while (cd.isArray()) { cd = cd.getComponentType(); } if (cd.isPrimitive()) continue; if (cd.getPackage().equals("java.lang")) continue; if (cd.getFullyQualifiedName().equals("java.util.List")) continue; imports.add(cd.getFullyQualifiedName()); } for (ClassData cd : returnTypes) { while (cd.isArray()) { cd = cd.getComponentType(); } if (cd.isPrimitive()) continue; if (cd.getPackage().equals("java.lang")) continue; if (cd.getFullyQualifiedName().equals("java.util.List")) continue; imports.add(cd.getFullyQualifiedName()); } bind.put("imports", imports); OutputLogger.getInstance() .log( MessageFormat.format( NbBundle.getMessage(ClientBeanGeneratorTemplate.class, "MSG_GenerateJavonClient"), FileUtil.toFile(outputFile))); // NOI18N Writer w = null; Reader is = null; try { try { w = new StringWriter(); is = new InputStreamReader(template.getInputStream()); eng.getContext().setWriter(w); eng.getContext() .setAttribute(FileObject.class.getName(), template, ScriptContext.ENGINE_SCOPE); eng.getContext() .setAttribute( ScriptEngine.FILENAME, template.getNameExt(), ScriptContext.ENGINE_SCOPE); eng.eval(is); } catch (FileNotFoundException e) { OutputLogger.getInstance().log(e); ErrorManager.getDefault().notify(e); return false; } catch (ScriptException e) { OutputLogger.getInstance().log(e); ErrorManager.getDefault().notify(e); return false; } finally { if (w != null) { off.write(w.toString()); // System.err.println( "" + w.toString()); w.close(); } if (is != null) is.close(); off.close(); } } catch (IOException e) { generationFailed(e, FileUtil.toFile(outputFile)); return false; } OutputLogger.getInstance() .log( MessageFormat.format( NbBundle.getMessage(ClientJavonTemplate.class, "MSG_ClientGenerated"), FileUtil.toFile(outputFile))); } return true; }
public static synchronized ServiceGeneratorResult generate(final E2EDataObject dataObject) { if (dataObject.getServerProject() == null) { final NotifyDescriptor.Message dd = new NotifyDescriptor.Message( NbBundle.getMessage( E2EDataObject.class, "ERR_ServerProjectNotOpened", // NOI18N dataObject.getConfiguration().getServerConfigutation().getProjectName())); DialogDisplayer.getDefault().notify(dd); if (Util.openProject(dataObject.getConfiguration().getServerConfigutation().getProjectPath()) == null) { // It is OK don't notify user here. All notifications // are already inside <code>openProject</code> method return null; } } // Call save before generate final SaveCookie saveCookie = dataObject.getCookie(SaveCookie.class); if (saveCookie != null) { try { saveCookie.save(); } catch (IOException ex) { ErrorManager.getDefault().notify(ex); } } // Get configuration final Configuration config = dataObject.getConfiguration(); if (config == null) { final NotifyDescriptor.Message dd = new NotifyDescriptor.Message( NbBundle.getMessage(E2EDataObject.class, "ERR_ConfigurationFileCorrupted")); // NOI18N DialogDisplayer.getDefault().notify(dd); return null; } if (Configuration.WSDLCLASS_TYPE.equals(config.getServiceType())) { final FileObject fo = dataObject .getServerProject() .getProjectDirectory() .getFileObject("build/generated/wsimport/"); // NOI18N if (fo == null) { DialogDisplayer.getDefault() .notify( new NotifyDescriptor.Message( NbBundle.getMessage( ConnectionGenerator.class, "MSG_WebProjectNotBuilt"))); // NOI18N return null; } } /* * All failures notifications was done via dialog windows. * Starting now logging will be done via OutputLogger class. */ final ProgressHandle ph = ProgressHandleFactory.createHandle( NbBundle.getMessage(ConnectionGenerator.class, "MSG_GeneratingJavonBridge"), // NOI18N new AbstractAction() { public void actionPerformed(ActionEvent e) { OutputLogger.getInstance().open(); } }); ph.start(); ph.switchToIndeterminate(); String message = NbBundle.getMessage(ConnectionGenerator.class, "MSG_GeneratingProxyStubs"); // NOI18N ph.progress(message); OutputLogger.getInstance().log(message); // FIXME: check for proper type // config.getServices(); // if (Configuration.WSDLCLASS_TYPE.equals(config.getServiceType())) { ph.progress(NbBundle.getMessage(ConnectionGenerator.class, "MSG_GeneratingProxyStubs")); final ProxyGenerator pg = new ProxyGenerator(dataObject); final String className = pg.generate(); if (className == null) { ph.finish(); StatusDisplayer.getDefault() .setStatusText(NbBundle.getMessage(ConnectionGenerator.class, "MSG_Failure")); // NOI18N return null; } config.getServices().get(0).getData().get(0).setProxyClassType(className); } // JavonOutput[] outputs; // Type type = null; // // final InputOutput io = IOProvider.getDefault().getIO( // NbBundle.getMessage( ConnectionGenerator.class, "LBL_JavonTab" ) // NOI18N // , true); // final OutputWriter ow = io.getOut(); try { String scanning = NbBundle.getMessage(ConnectionGenerator.class, "MSG_ScanningDataStructures"); // NOI18N ph.progress(scanning); OutputLogger.getInstance().log(scanning); final JavonMappingImpl mapping = dataObject.getMapping(); if (Configuration.WSDLCLASS_TYPE.equals(config.getServiceType())) { mapping.setProperty("serviceType", "WSDL"); } else { mapping.setProperty("serviceType", "CLASS"); } // //ph.progress(70); String creating = NbBundle.getMessage(ConnectionGenerator.class, "MSG_CreatingJavaFiles"); // NOI18N ph.progress(creating); OutputLogger.getInstance().log(creating); // Javon javon = new Javon(mapping); if (javon.generate(ph)) { StatusDisplayer.getDefault() .setStatusText( NbBundle.getMessage(ConnectionGenerator.class, "MSG_SuccessGenerated")); // NOI18N OutputLogger.getInstance() .log(NbBundle.getMessage(ConnectionGenerator.class, "TXT_GenerationSuccess")); // NOI18N } else { StatusDisplayer.getDefault() .setStatusText(NbBundle.getMessage(ConnectionGenerator.class, "MSG_Failure")); // NOI18N OutputLogger.getInstance() .log( NbBundle.getMessage( ConnectionGenerator.class, "TXT_GenerationUnsuccess")); // NOI18N } // Streams.setOut(ow); // Streams.setErr(ow); // outputs = new Main().run( mapping, "" ); // NOI18N // // for( int j = 0; j < outputs.length; j++ ) { // final String list[] = outputs[j].getCreatedFiles(); // for( int i = 0; i < list.length; i++ ) { // final File f = new File(list[i]); // final FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(f)); // fo.refresh(); // JavaModel.getResource(fo); // } // } // //add servlet to container // Util.addServletToWebProject(dataObject.getServerProject(), // dataObject.getConfiguration().getServerConfigutation().getClassDescriptor().getType()); // // final ClassDescriptor clientClassDescriptor = // dataObject.getConfiguration().getClientConfiguration().getClassDescriptor(); // final Sources s = ProjectUtils.getSources(dataObject.getClientProject()); // final SourceGroup sourceGroup = Util.getPreselectedGroup( // s.getSourceGroups( JavaProjectConstants.SOURCES_TYPE_JAVA ), // clientClassDescriptor.getLocation()); // final FileObject srcDirectory = sourceGroup.getRootFolder(); // final ClassPath cp = ClassPath.getClassPath(srcDirectory,ClassPath.SOURCE); // JavaModel.getJavaRepository().beginTrans(false); // try { // JavaModel.setClassPath(cp); // type = // JavaModel.getDefaultExtent().getType().resolve(clientClassDescriptor.getType()); // } catch (Exception e){ // ErrorManager.getDefault().notify(e); // } finally { // JavaModel.getJavaRepository().endTrans(); // } } finally { ph.finish(); OutputLogger.getInstance().close(); } // if (type != null){ // //ow.println("Run / Redeploy Web Project to get changes reflected!"); // StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage( // ConnectionGenerator.class, "MSG_SuccessGenerated" )); // NOI18N // } else { // StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage( // ConnectionGenerator.class, "MSG_Failure" )); // NOI18N // return null; // } // final JavaClass resultClass = (JavaClass)type; // final List<Feature> features = resultClass.getFeatures(); // final List<Method> methods = new ArrayList<Method>(); // for ( final Feature elem : features ) { // if (elem instanceof Method){ // final Method m = (Method)elem; // if ("getGroupedResults".equals(m.getName())){ //NOI18N //not supported // continue; // } // if (m.getName().endsWith("Grouped")){ //NOI18N //not supported // continue; // } // if ( Modifier.isPublic(m.getModifiers()) ){ // methods.add(m); // } // } // } // return new ServiceGeneratorResult(resultClass, // methods.toArray(new Method[methods.size()]), // Util.getServerURL(dataObject.getServerProject(), // dataObject.getConfiguration())); return null; }