/** * Creates the default CASA file object in the JBI project. * * @param project a JBI project * @return the newly created CASA file object */ public static FileObject createDefaultCasaFileObject(JbiProject project) { ProjectInformation projInfo = project.getLookup().lookup(ProjectInformation.class); assert projInfo != null; String projName = projInfo.getName(); FileObject confFO = project.getProjectDirectory().getFileObject(CASA_DIR_NAME); FileObject casaFO = null; try { FileObject casaTemplateFO = FileUtil.getConfigFile( "org-netbeans-modules-compapp-projects-jbi/project.casa" // NOI18N ); casaFO = FileUtil.copyFile(casaTemplateFO, confFO, projName); // registerCasaFileListener(project); } catch (IOException ex) { ex.printStackTrace(); } return casaFO; }
/** * Gets the CASA file object in the given JBI project. * * @param project a JBI project * @param create if <code>true</code> and the CASA file doesn't exist in the project, then an * empty CASA file will be created * @return CASA file object */ public static FileObject getCasaFileObject(JbiProject project, boolean create) { ProjectInformation projInfo = project.getLookup().lookup(ProjectInformation.class); assert projInfo != null; String projName = projInfo.getName(); FileObject confFO = project.getProjectDirectory().getFileObject(CASA_DIR_NAME); if (confFO == null) { // This could happen during compapp rename with directory name change. return null; } FileObject casaFO = confFO.getFileObject(projName + CASA_EXT); if (casaFO == null && create) { casaFO = createDefaultCasaFileObject(project); updateCasaWithJBIModules(project); } return casaFO; }
/** * Updates CASA FileObject with service engine service unit info defined in the project * properties. * * @param project a JBI project * @param properties project properties (may not been persisted yet) */ public static void updateCasaWithJBIModules(JbiProject project, JbiProjectProperties properties) { FileObject casaFO = CasaHelper.getCasaFileObject(project, true); if (casaFO == null) { return; } try { boolean modified = false; // whether casa is modified DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document casaDocument = builder.parse(casaFO.getInputStream()); Element sus = (Element) casaDocument.getElementsByTagName(CasaConstants.CASA_SERVICE_UNITS_ELEM_NAME).item(0); NodeList seSUs = sus.getElementsByTagName(CasaConstants.CASA_SERVICE_ENGINE_SERVICE_UNIT_ELEM_NAME); @SuppressWarnings("unchecked") List<VisualClassPathItem> newContentList = (List) properties.get(JbiProjectProperties.JBI_CONTENT_ADDITIONAL); @SuppressWarnings("unchecked") List<String> newTargetIDs = (List) properties.get(JbiProjectProperties.JBI_CONTENT_COMPONENT); List<String> newProjectNameList = new ArrayList<String>(); for (VisualClassPathItem newContent : newContentList) { newProjectNameList.add(newContent.getProjectName()); } List<String> sesuNames = new ArrayList<String>(); for (int i = 0; i < seSUs.getLength(); i++) { Element seSU = (Element) seSUs.item(i); String unitName = seSU.getAttribute(CasaConstants.CASA_UNIT_NAME_ATTR_NAME); sesuNames.add(unitName); } // Remove deleted service units from casa for (int i = 0; i < seSUs.getLength(); i++) { Element seSU = (Element) seSUs.item(i); // skip external unknown SUs String internal = seSU.getAttribute(CasaConstants.CASA_INTERNAL_ATTR_NAME); String unknown = seSU.getAttribute(CasaConstants.CASA_UNKNOWN_ATTR_NAME); if ("false".equalsIgnoreCase(internal) && "true".equals(unknown)) { // NOI18N continue; } String projName = seSU.getAttribute(CasaConstants.CASA_UNIT_NAME_ATTR_NAME); if (!newProjectNameList.contains(projName)) { sus.removeChild(seSU); modified = true; // System.out.println("removing old su: " + projName); } } // Add new service units to casa List<String> externalSuNames = project.getExternalServiceUnitNames(); for (VisualClassPathItem artifact : newContentList) { String projName = artifact.getProjectName(); String artifactName = artifact.toString(); if (!sesuNames.contains(projName)) { String targetCompID = "unknown"; // NOI18N for (int j = 0; j < newContentList.size(); j++) { if (newContentList.get(j).toString().equals(artifactName)) { targetCompID = newTargetIDs.get(j); break; } } Element seSU = casaDocument.createElement(CasaConstants.CASA_SERVICE_ENGINE_SERVICE_UNIT_ELEM_NAME); seSU.setAttribute(CasaConstants.CASA_X_ATTR_NAME, "-1"); // NOI18N seSU.setAttribute(CasaConstants.CASA_Y_ATTR_NAME, "-1"); // NOI18N seSU.setAttribute( CasaConstants.CASA_INTERNAL_ATTR_NAME, externalSuNames.contains(projName) ? "false" : "true"); // NOI18N seSU.setAttribute(CasaConstants.CASA_DEFINED_ATTR_NAME, "false"); // NOI18N seSU.setAttribute(CasaConstants.CASA_UNKNOWN_ATTR_NAME, "false"); // NOI18N seSU.setAttribute(CasaConstants.CASA_NAME_ATTR_NAME, projName); // NOI18N // FIXME seSU.setAttribute(CasaConstants.CASA_UNIT_NAME_ATTR_NAME, projName); // NOI18N seSU.setAttribute(CasaConstants.CASA_COMPONENT_NAME_ATTR_NAME, targetCompID); // NOI18N seSU.setAttribute(CasaConstants.CASA_DESCRIPTION_ATTR_NAME, "some description"); // NOI18N seSU.setAttribute(CasaConstants.CASA_ARTIFACTS_ZIP_ATTR_NAME, artifactName); sus.appendChild(seSU); modified = true; // System.out.println("Adding new su: " + projName); } } if (modified) { // System.out.println("CasaHelper: starting writing to CASA (Thread:" + // Thread.currentThread().getName() + ")"); XmlUtil.writeToFileObject(casaFO, casaDocument); // System.out.println("CasaHelper: finished writing to CASA (Thread:" + // Thread.currentThread().getName() + ")"); } } catch (Exception e) { e.printStackTrace(); } }
/** * Updates CASA FileObject with service engine service unit info defined in the project * properties. * * @param project a JBI project */ public static void updateCasaWithJBIModules(JbiProject project) { JbiProjectProperties properties = project.getProjectProperties(); updateCasaWithJBIModules(project, properties); }