Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.java.j2seproject; |
43 |
|
44 |
import java.io.IOException; |
45 |
import org.netbeans.api.annotations.common.CheckForNull; |
46 |
import org.netbeans.api.annotations.common.NonNull; |
47 |
import org.netbeans.api.java.platform.JavaPlatform; |
48 |
import org.netbeans.api.java.platform.JavaPlatformManager; |
49 |
import org.netbeans.api.project.ProjectManager; |
50 |
import org.netbeans.modules.java.api.common.ant.UpdateHelper; |
51 |
import org.netbeans.modules.java.api.common.util.CommonProjectUtils; |
52 |
import org.netbeans.modules.java.j2seproject.api.J2SEProjectPlatform; |
53 |
import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; |
54 |
import org.netbeans.spi.project.support.ant.AntProjectHelper; |
55 |
import org.netbeans.spi.project.support.ant.EditableProperties; |
56 |
import org.openide.modules.SpecificationVersion; |
57 |
import org.openide.util.Mutex; |
58 |
import org.openide.util.MutexException; |
59 |
import org.openide.util.Parameters; |
60 |
import org.openide.xml.XMLUtil; |
61 |
import org.w3c.dom.Element; |
62 |
|
63 |
/** |
64 |
* |
65 |
* @author Tomas Zezula |
66 |
*/ |
67 |
class J2SEProjectPlatformImpl implements J2SEProjectPlatform { |
68 |
|
69 |
private static final String SE_PLATFORM = "j2se"; //NOI18N |
70 |
private static final String PROP_PLATFORM_ANT_NAME = "platform.ant.name"; //NOI18N |
71 |
|
72 |
private final J2SEProject project; |
73 |
|
74 |
J2SEProjectPlatformImpl(@NonNull final J2SEProject project) { |
75 |
assert project != null; |
76 |
this.project = project; |
77 |
} |
78 |
|
79 |
@Override |
80 |
@CheckForNull |
81 |
public JavaPlatform getProjectPlatform() { |
82 |
return ProjectManager.mutex().readAccess(new Mutex.Action<JavaPlatform>(){ |
83 |
@Override |
84 |
public JavaPlatform run() { |
85 |
return CommonProjectUtils.getActivePlatform( |
86 |
project.evaluator().getProperty(J2SEProjectProperties.JAVA_PLATFORM)); |
87 |
} |
88 |
}); |
89 |
} |
90 |
|
91 |
@Override |
92 |
public void setProjectPlatform(@NonNull final JavaPlatform platform) throws IOException { |
93 |
Parameters.notNull("platform", platform); |
94 |
if (!SE_PLATFORM.equals(platform.getSpecification().getName())) { |
95 |
throw new IllegalArgumentException( |
96 |
String.format( |
97 |
"Not J2SE Platform: %s (%s)", //NOI18N |
98 |
platform.getDisplayName(), |
99 |
platform.getSpecification().getName())); |
100 |
} |
101 |
if (platform.getInstallFolders().isEmpty()) { |
102 |
throw new IllegalArgumentException( |
103 |
String.format( |
104 |
"Broken Platform %s", //NOI18N |
105 |
platform.getDisplayName())); |
106 |
} |
107 |
try { |
108 |
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() { |
109 |
@Override |
110 |
public Void run() throws IOException { |
111 |
final String platformId = platform.getProperties().get(PROP_PLATFORM_ANT_NAME); |
112 |
final UpdateHelper uh = project.getUpdateHelper(); |
113 |
final EditableProperties props = uh.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); |
114 |
props.setProperty(J2SEProjectProperties.JAVA_PLATFORM, platformId); |
115 |
uh.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); |
116 |
updateProjectXml(platform, uh); |
117 |
ProjectManager.getDefault().saveProject(project); |
118 |
return null; |
119 |
} |
120 |
}); |
121 |
} catch (MutexException e) { |
122 |
throw (IOException) e.getCause(); |
123 |
} |
124 |
} |
125 |
|
126 |
static boolean updateProjectXml( |
127 |
@NonNull final JavaPlatform platform, |
128 |
@NonNull final UpdateHelper helper) { |
129 |
assert ProjectManager.mutex().isWriteAccess(); |
130 |
final boolean remove = platform.equals(JavaPlatformManager.getDefault().getDefaultPlatform()); |
131 |
final Element root = helper.getPrimaryConfigurationData(true); |
132 |
boolean changed = false; |
133 |
if (remove) { |
134 |
final Element platformElement = XMLUtil.findElement( |
135 |
root, |
136 |
"explicit-platform", //NOI18N |
137 |
J2SEProject.PROJECT_CONFIGURATION_NAMESPACE); |
138 |
if (platformElement != null) { |
139 |
root.removeChild(platformElement); |
140 |
changed = true; |
141 |
} |
142 |
} else { |
143 |
Element insertBefore = null; |
144 |
for (Element e : XMLUtil.findSubElements(root)) { |
145 |
final String name = e.getNodeName(); |
146 |
if (! "name".equals(name) && //NOI18N |
147 |
! "minimum-ant-version".equals(name)) { //NOI18N |
148 |
insertBefore = e; |
149 |
break; |
150 |
} |
151 |
} |
152 |
final Element platformNode = insertBefore.getOwnerDocument().createElementNS( |
153 |
J2SEProject.PROJECT_CONFIGURATION_NAMESPACE, |
154 |
"explicit-platform"); //NOI18N |
155 |
platformNode.setAttribute( |
156 |
"explicit-source-supported", //NOI18N |
157 |
platform.getSpecification().getVersion().compareTo(new SpecificationVersion("1.3"))>0? //NOI18N |
158 |
"true": //NOI18N |
159 |
"false"); //NOI18N |
160 |
root.insertBefore(platformNode, insertBefore); |
161 |
changed = true; |
162 |
} |
163 |
if (changed) { |
164 |
helper.putPrimaryConfigurationData(root, true); |
165 |
} |
166 |
return changed; |
167 |
} |
168 |
|
169 |
} |