1 19 20 package org.netbeans.modules.projectimport.eclipse; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import java.util.logging.Logger ; 33 import org.netbeans.api.java.platform.JavaPlatform; 34 import org.netbeans.api.java.platform.JavaPlatformManager; 35 import org.netbeans.api.java.project.JavaProjectConstants; 36 import org.netbeans.api.project.ProjectManager; 37 import org.netbeans.api.project.ant.AntArtifact; 38 import org.netbeans.api.project.ant.AntArtifactQuery; 39 import org.netbeans.modules.java.j2seplatform.platformdefinition.PlatformConvertor; 40 import org.netbeans.modules.java.j2seplatform.wizard.NewJ2SEPlatform; 41 import org.netbeans.modules.java.j2seproject.J2SEProject; 42 import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator; 43 import org.netbeans.modules.java.j2seproject.J2SEProjectType; 44 import org.netbeans.modules.java.j2seproject.SourceRoots; 45 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 46 import org.netbeans.modules.projectimport.LoggerFactory; 47 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 48 import org.netbeans.spi.project.support.ant.AntProjectHelper; 49 import org.netbeans.spi.project.support.ant.EditableProperties; 50 import org.netbeans.spi.project.support.ant.PropertyUtils; 51 import org.openide.ErrorManager; 52 import org.openide.filesystems.FileObject; 53 import org.openide.filesystems.FileUtil; 54 import org.openide.filesystems.Repository; 55 import org.openide.loaders.DataFolder; 56 import org.openide.loaders.DataObject; 57 import org.openide.util.NbBundle; 58 import org.openide.util.RequestProcessor; 59 import org.w3c.dom.Element ; 60 61 69 final class Importer { 70 71 72 private static final Logger logger = 73 LoggerFactory.getDefault().createLogger(Importer.class); 74 75 private final Set eclProjects; 76 private final String destination; 77 private final boolean recursively; 78 private J2SEProject[] nbProjects; 79 private final Set recursionCheck; 80 private final Map loadedProject; 81 82 private int nOfProcessed; 83 private String progressInfo; 84 private boolean done; 85 private Collection warnings; 86 87 private JavaPlatform[] nbPlfs; private File nbDefPlfFile; 90 Importer(final Set eclProjects, String destination, boolean recursively) { 91 this.eclProjects = eclProjects; 92 this.destination = destination; 93 this.recursively = recursively; 94 this.nbProjects = new J2SEProject[eclProjects.size()]; 95 recursionCheck = new HashSet (); 96 loadedProject = new HashMap (); 97 } 98 99 103 void startImporting() { 104 nbPlfs = JavaPlatformManager.getDefault().getInstalledPlatforms(); 105 JavaPlatform defPlf = JavaPlatformManager.getDefault().getDefaultPlatform(); 106 Collection installFolder = defPlf.getInstallFolders(); 107 if (installFolder.isEmpty()) { 108 logWarning(NbBundle.getMessage(Importer.class, "MSG_NotValidPlatformsInNB")); return; 110 } else { 111 nbDefPlfFile = FileUtil.toFile((FileObject) installFolder.toArray()[0]); 112 } 113 RequestProcessor.getDefault().post(new Runnable () { 114 public void run() { 115 ProjectManager.mutex().writeAccess(new Runnable () { 116 public void run() { 117 try { 118 int pos = 0; 119 for (Iterator it = eclProjects.iterator(); it.hasNext(); ) { 120 EclipseProject eclPrj = (EclipseProject) it.next(); 121 nbProjects[pos++] = importProject(eclPrj); 122 } 123 } catch (IOException ioe) { 124 Throwable t = ErrorManager.getDefault().annotate(ioe, 125 "Error occured during project importing"); ErrorManager.getDefault().notify(ErrorManager.USER, t); 127 } finally { 128 done = true; 129 } 130 } 131 }); 132 } 133 }); 134 } 135 136 139 int getNOfProcessed() { 140 return nOfProcessed; 141 } 142 143 146 String getProgressInfo() { 147 return progressInfo; 148 } 149 150 153 boolean isDone() { 154 return done; 155 } 156 157 Collection getWarnings() { 158 return warnings; 159 } 160 161 164 J2SEProject[] getProjects() { 165 return nbProjects; 166 } 167 168 private J2SEProject importProject(EclipseProject eclProject) throws IOException { 169 assert eclProject != null : "Eclipse project cannot be null"; 171 if (!recursionCheck.add(eclProject.getDirectory().toString())) { 173 J2SEProject project = (J2SEProject) loadedProject.get( 174 eclProject.getDirectory().getAbsolutePath()); 175 return project; 176 } 177 logger.finer("Importing of project: \"" + eclProject.getDirectory().getAbsolutePath() + "\" started"); nOfProcessed++; 180 progressInfo = NbBundle.getMessage(Importer.class, 181 "MSG_Progress_ProcessingProject", eclProject.getName()); File nbProjectDir = FileUtil.normalizeFile(new File (destination + "/" + eclProject.getName())); Map eclRoots = eclProject.getAllSourceRoots(); 184 File [] testDirs = new File [0]; 185 File [] srcFiles = new File [eclRoots.size()]; 186 int j = 0; 187 for (Iterator it = eclRoots.keySet().iterator(); it.hasNext(); ) { 188 srcFiles[j++] = (File ) it.next(); 189 } 190 final AntProjectHelper helper = J2SEProjectGenerator.createProject( 192 nbProjectDir, eclProject.getName(), srcFiles, testDirs, null); 193 J2SEProject nbProject = (J2SEProject) ProjectManager.getDefault(). 195 findProject(FileUtil.toFileObject( 196 FileUtil.normalizeFile(nbProjectDir))); 197 ProjectClassPathExtender nbProjectClassPath = 198 (ProjectClassPathExtender) nbProject.getLookup().lookup(ProjectClassPathExtender.class); 199 assert nbProjectClassPath != null : "Cannot lookup ProjectClassPathExtender"; 201 SourceRoots roots = nbProject.getSourceRoots(); 203 URL [] rootURLs = roots.getRootURLs(); 204 String [] labels = new String [rootURLs.length]; 205 for (int i = 0; i < rootURLs.length; i++) { 206 labels[i] = (String ) eclRoots.get(new File (rootURLs[i].getFile())); 207 } 208 roots.putRoots(rootURLs, labels); 209 210 for (Iterator it = eclProject.getAllLibrariesFiles().iterator(); it.hasNext(); ) { 212 File eclLib = (File ) it.next(); 213 if (eclLib.exists()) { 214 nbProjectClassPath.addArchiveFile(FileUtil.toFileObject( 215 FileUtil.normalizeFile(eclLib))); 216 } else { 217 logWarning(NbBundle.getMessage(Importer.class, "MSG_LibraryDoesnExist", eclProject.getName(), eclLib.getAbsolutePath()), true); 219 } 220 } 221 222 if (recursively) { 224 Collection projects = eclProject.getProjects(); 225 for (Iterator it = projects.iterator(); it.hasNext(); ) { 226 EclipseProject eclSubProject = (EclipseProject) it.next(); 227 J2SEProject nbSubProject = importProject(eclSubProject); 228 if (nbSubProject != null) { 231 AntArtifact[] artifact = 232 AntArtifactQuery.findArtifactsByType(nbSubProject, 233 JavaProjectConstants.ARTIFACT_TYPE_JAR); 234 nbProjectClassPath.addAntArtifact( 235 artifact[0], artifact[0].getArtifactLocations()[0]); 236 } else { 237 logger.warning("Project in directory \"" + eclProject.getDirectory().getAbsolutePath() + 239 "\" is already being processed. Recursive " + "dependencies reached. "); } 242 } 243 } 244 245 setJavaPlatform(eclProject, helper); 247 248 ProjectManager.getDefault().saveProject(nbProject); 249 logger.finer("Project loaded: " + eclProject.getDirectory().getAbsolutePath()); 251 loadedProject.put(eclProject.getDirectory().getAbsolutePath(), nbProject); 252 return nbProject; 253 } 254 255 256 private void setJavaPlatform(EclipseProject eclProject, 257 final AntProjectHelper helper) throws IOException { 258 String eclPlfDir = eclProject.getJDKDirectory(); 260 if (eclPlfDir == null) { 264 return; 265 } 266 File eclPlfFile = FileUtil.normalizeFile(new File (eclPlfDir)); 267 if (eclPlfFile.equals(nbDefPlfFile)) { return; 269 } 270 JavaPlatform nbPlf = null; 271 for (int i = 0; i < nbPlfs.length; i++) { 272 JavaPlatform current = nbPlfs[i]; 273 Collection instFolders = current.getInstallFolders(); 274 if (instFolders.isEmpty()) { 275 logger.fine("Java platform \"" + current.getDisplayName() + "\" is not valid. Skipping..."); continue; 278 } 279 File nbPlfDir = FileUtil.toFile((FileObject) instFolders.toArray()[0]); 280 if (nbPlfDir.equals(eclPlfFile)) { 281 nbPlf = nbPlfs[i]; 282 break; 284 } 285 } 286 if (nbPlf == null) { 291 logger.fine("Creating new platform: " + eclPlfFile.getAbsolutePath()); FileObject fo = FileUtil.toFileObject(eclPlfFile); 293 if (fo != null) { 294 NewJ2SEPlatform plat = NewJ2SEPlatform.create(fo); 295 plat.run(); 296 if (plat.isValid()) { 297 if (plat.findTool("javac")!= null) { String displayName = createPlatformDisplayName(plat); 299 String antName = createPlatformAntName(displayName); 300 plat.setDisplayName(displayName); 301 plat.setAntName(antName); 302 FileObject platformsFolder = Repository.getDefault(). 303 getDefaultFileSystem().findResource( 304 "Services/Platforms/org-netbeans-api-java-Platform"); assert platformsFolder != null; 306 DataObject dobj = PlatformConvertor.create(plat, 307 DataFolder.findFolder(platformsFolder), antName); 308 nbPlf = (JavaPlatform) dobj.getNodeDelegate().getLookup(). 309 lookup(JavaPlatform.class); 310 nbPlfs = JavaPlatformManager.getDefault().getInstalledPlatforms(); 312 } else { 313 logWarning(NbBundle.getMessage(Importer.class, "MSG_JRECannotBeUsed", eclProject.getName()), true); 315 } 316 } else { 317 logWarning( "Cannot create new J2SE platform, the " + "default platform will be used."); } 323 } else { 324 logWarning(NbBundle.getMessage(Importer.class, "MSG_JDKDoesnExistUseDefault", eclProject.getName(), eclPlfFile.getAbsolutePath()), true); 326 } 327 } 328 if (nbPlf != null) { 331 Element pcd = helper.getPrimaryConfigurationData(true); 332 Element el = pcd.getOwnerDocument().createElementNS( 333 J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE, 334 "explicit-platform"); pcd.appendChild(el); 336 helper.putPrimaryConfigurationData(pcd, true); 337 EditableProperties prop = 338 helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 339 String ver = nbPlf.getSpecification().getVersion().toString(); 340 String normalizedName = (String )nbPlf.getProperties().get( 341 "platform.ant.name"); prop.setProperty(J2SEProjectProperties.JAVAC_SOURCE, ver); 343 prop.setProperty(J2SEProjectProperties.JAVAC_TARGET, ver); 344 prop.setProperty(J2SEProjectProperties.JAVA_PLATFORM, normalizedName); 345 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, prop); 346 } else { 347 logWarning("Setting of platform for project \"" + eclProject.getName() + "\" failed."); } 350 } 351 352 private void logWarning(String message) { 353 logWarning(message, false); 354 } 355 356 360 private void logWarning(String message, boolean isGUIWarning) { 361 if (isGUIWarning) { 362 if (warnings == null) { 363 warnings = new ArrayList (); 364 } 365 warnings.add(message); 366 } 367 logger.warning(message); 368 } 369 370 371 private static String createPlatformDisplayName(JavaPlatform plat) { 372 Map m = plat.getSystemProperties(); 373 String vmName = (String )m.get("java.vm.name"); String vmVersion = (String )m.get("java.vm.version"); StringBuffer displayName = new StringBuffer (); 376 if (vmName != null) { 377 displayName.append(vmName); 378 } 379 if (vmVersion != null) { 380 if (displayName.length() > 0) { 381 displayName.append(' '); 382 } 383 displayName.append(vmVersion); 384 } 385 return displayName.toString(); 386 } 387 388 private String createPlatformAntName(String displayName) { 389 assert displayName != null && displayName.length() > 0; 390 String antName = PropertyUtils.getUsablePropertyName(displayName); 391 if (platformExists(antName)) { 392 String baseName = antName; 393 int index = 1; 394 antName = baseName + Integer.toString(index); 395 while (platformExists(antName)) { 396 index ++; 397 antName = baseName + Integer.toString(index); 398 } 399 } 400 return antName; 401 } 402 403 406 private boolean platformExists(String antName) { 407 assert antName != null && antName.length() > 0; 408 for (int i=0; i< nbPlfs.length; i++) { 409 String otherName = (String ) nbPlfs[i].getProperties().get("platform.ant.name"); if (antName.equals(otherName)) { 411 return true; 412 } 413 } 414 return false; 415 } 416 417 } 418 | Popular Tags |