PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/maven/src/org/netbeans/modules/maven/api/customizer/ModelHandle2.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 475 lines | 288 code | 71 blank | 116 comment | 31 complexity | 1b78455736625ad95d540c8f0fca9b78 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2012 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 2012 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.maven.api.customizer;
  43. import java.io.IOException;
  44. import java.io.StringReader;
  45. import java.util.ArrayList;
  46. import java.util.Arrays;
  47. import java.util.HashSet;
  48. import java.util.Iterator;
  49. import java.util.List;
  50. import java.util.Map;
  51. import java.util.Set;
  52. import java.util.TreeMap;
  53. import org.apache.maven.project.MavenProject;
  54. import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
  55. import org.netbeans.api.annotations.common.CheckForNull;
  56. import org.netbeans.api.project.Project;
  57. import org.netbeans.modules.maven.MavenProjectPropsImpl;
  58. import static org.netbeans.modules.maven.api.customizer.Bundle.*;
  59. import org.netbeans.modules.maven.configurations.M2Configuration;
  60. import org.netbeans.modules.maven.customizer.CustomizerProviderImpl;
  61. import org.netbeans.modules.maven.execute.ActionToGoalUtils;
  62. import org.netbeans.modules.maven.execute.model.ActionToGoalMapping;
  63. import org.netbeans.modules.maven.execute.model.NetbeansActionMapping;
  64. import org.netbeans.modules.maven.execute.model.io.xpp3.NetbeansBuildActionXpp3Reader;
  65. import org.netbeans.modules.maven.model.ModelOperation;
  66. import org.netbeans.modules.maven.model.pom.POMModel;
  67. import org.netbeans.spi.project.ProjectConfiguration;
  68. import org.openide.util.NbBundle.Messages;
  69. /**
  70. *
  71. * @author mkleint
  72. */
  73. public class ModelHandle2 {
  74. public static final String PANEL_RUN = "RUN"; //NOI18N
  75. public static final String PANEL_BASIC = "BASIC"; //NOI18N
  76. public static final String PANEL_CONFIGURATION = "CONFIGURATION"; //NOI18N
  77. public static final String PANEL_MAPPING = "MAPPING"; //NOI18N
  78. public static final String PANEL_LIBRARIES = "LIBRARIES"; //NOI18N
  79. public static final String PANEL_SOURCES = "SOURCES"; //NOI18N
  80. public static final String PANEL_COMPILE = "COMPILE"; //NOI18N
  81. private final MavenProjectPropsImpl auxiliaryProps;
  82. private final MavenProject project;
  83. private final POMModel model;
  84. private final List<ModelOperation<POMModel>> pomOperations = new ArrayList<ModelOperation<POMModel>>();
  85. private final Map<String, ActionToGoalMapping> mappings;
  86. private List<Configuration> configurations;
  87. private boolean modConfig = false;
  88. private Configuration active;
  89. TreeMap<String, String> transPropsShared = new TreeMap<String, String>();
  90. TreeMap<String, String> transPropsPrivate = new TreeMap<String, String>();
  91. Set<ActionToGoalMapping> modifiedMappings = new HashSet<ActionToGoalMapping>();
  92. static {
  93. ModelHandle2.AccessorImpl impl = new ModelHandle2.AccessorImpl();
  94. impl.assign();
  95. }
  96. static class AccessorImpl extends CustomizerProviderImpl.ModelAccessor2 {
  97. public @Override ModelHandle2 createHandle(
  98. POMModel model,
  99. MavenProject proj,
  100. Map<String, ActionToGoalMapping> mapp,
  101. List<Configuration> configs,
  102. Configuration active,
  103. MavenProjectPropsImpl auxProps) {
  104. return new ModelHandle2(model, proj, mapp, configs, active, auxProps);
  105. }
  106. public void assign() {
  107. if (CustomizerProviderImpl.ACCESSOR2 == null) {
  108. CustomizerProviderImpl.ACCESSOR2 = this;
  109. }
  110. }
  111. @Override
  112. public TreeMap<String, String> getModifiedAuxProps(ModelHandle2 handle, boolean shared) {
  113. return handle.getModifiedAuxProps(shared);
  114. }
  115. @Override
  116. public boolean isConfigurationModified(ModelHandle2 handle) {
  117. return handle.modConfig;
  118. }
  119. @Override
  120. public boolean isModified(ModelHandle2 handle, ActionToGoalMapping mapp) {
  121. return handle.modifiedMappings.contains(mapp);
  122. }
  123. }
  124. private ModelHandle2(POMModel model, MavenProject proj, Map<String, ActionToGoalMapping> mapp, List<Configuration> configs, Configuration active, MavenProjectPropsImpl auxProps) {
  125. project = proj;
  126. this.mappings = mapp;
  127. configurations = configs;
  128. this.active = active;
  129. auxiliaryProps = auxProps;
  130. this.model = model;
  131. }
  132. /**
  133. * pom.xml model, READ-ONLY
  134. * @return
  135. */
  136. public POMModel getPOMModel() {
  137. return model;
  138. }
  139. /**
  140. * the non changed (not-to-be-changed) instance of the complete project.
  141. * NOT TO BE CHANGED.
  142. * @return
  143. */
  144. public MavenProject getProject() {
  145. return project;
  146. }
  147. /**
  148. * get the value of Auxiliary property defined in the project,
  149. * however take only the content in nb-configurations.xml file into account, never
  150. * consider values from pom.xml here.
  151. * @param propertyName
  152. * @param shared
  153. * @return
  154. */
  155. public String getRawAuxiliaryProperty(String propertyName, boolean shared) {
  156. if (shared && transPropsShared.containsKey(propertyName)) {
  157. return transPropsShared.get(propertyName);
  158. }
  159. if (!shared && transPropsPrivate.containsKey(propertyName)) {
  160. return transPropsPrivate.get(propertyName);
  161. }
  162. return auxiliaryProps.get(propertyName, shared, false);
  163. }
  164. /**
  165. * set the value of Auxiliary property, will be written to nb-configurations.xml file
  166. * @param propertyName
  167. * @param shared
  168. * @param value
  169. */
  170. public void setRawAuxiliaryProperty(String propertyName, String value, boolean shared) {
  171. if (shared) {
  172. transPropsShared.put(propertyName, value);
  173. } else {
  174. transPropsPrivate.put(propertyName, value);
  175. }
  176. }
  177. private TreeMap<String, String> getModifiedAuxProps(boolean shared) {
  178. if (shared) return transPropsShared;
  179. else return transPropsPrivate;
  180. }
  181. /**
  182. * action mapping model
  183. * @return
  184. */
  185. public ActionToGoalMapping getActionMappings() {
  186. return mappings.get(M2Configuration.DEFAULT);
  187. }
  188. /**
  189. * action mapping model
  190. * @param config
  191. * @return
  192. */
  193. public ActionToGoalMapping getActionMappings(Configuration config) {
  194. ActionToGoalMapping mapp = mappings.get(config.getId());
  195. if (mapp == null) {
  196. mapp = new ActionToGoalMapping();
  197. markAsModified(mapp);
  198. mappings.put(config.getId(), mapp);
  199. }
  200. return mapp;
  201. }
  202. /**
  203. * always after modifying the models, mark them as modified.
  204. * without the marking, the particular file will not be saved.
  205. */
  206. public void markAsModified(ActionToGoalMapping mapp) {
  207. modifiedMappings.add(mapp);
  208. }
  209. /**
  210. * always after modifying the models, mark them as modified.
  211. * without the marking, the particular file will not be saved.
  212. */
  213. public void markConfigurationsAsModified() {
  214. modConfig = true;
  215. }
  216. public List<Configuration> getConfigurations() {
  217. return configurations;
  218. }
  219. public void addConfiguration(Configuration config) {
  220. assert config != null;
  221. configurations.add(config);
  222. modConfig = true;
  223. }
  224. public void removeConfiguration(Configuration config) {
  225. assert config != null;
  226. configurations.remove(config);
  227. if (active == config) {
  228. active = configurations.size() > 0 ? configurations.get(0) : null;
  229. }
  230. modConfig = true;
  231. }
  232. public Configuration getActiveConfiguration() {
  233. return active;
  234. }
  235. public void setActiveConfiguration(Configuration conf) {
  236. assert conf != null;
  237. assert configurations.contains(conf);
  238. active = conf;
  239. }
  240. public void addPOMModification(ModelOperation<POMModel> operation) {
  241. if (!pomOperations.contains(operation)) {
  242. pomOperations.add(operation);
  243. }
  244. }
  245. public void removePOMModification(ModelOperation<POMModel> operation) {
  246. pomOperations.remove(operation);
  247. }
  248. public List<ModelOperation<POMModel>> getPOMOperations() {
  249. return new ArrayList<ModelOperation<POMModel>>(pomOperations);
  250. }
  251. public static Configuration createProfileConfiguration(String id) {
  252. return ModelHandle.createProfileConfiguration(id);
  253. }
  254. public static Configuration createDefaultConfiguration() {
  255. return ModelHandle.createDefaultConfiguration();
  256. }
  257. public static Configuration createCustomConfiguration(String id) {
  258. return ModelHandle.createCustomConfiguration(id);
  259. }
  260. /**
  261. * inserts the action definition in the right place based on matching action name.
  262. * replaces old defintion or appends at the end.
  263. *
  264. * @param action
  265. * @param mapp
  266. */
  267. public static void setUserActionMapping(NetbeansActionMapping action, ActionToGoalMapping mapp) {
  268. action.setPackagings(null);
  269. List<NetbeansActionMapping> lst = mapp.getActions() != null ? mapp.getActions() : new ArrayList<NetbeansActionMapping>();
  270. Iterator<NetbeansActionMapping> it = lst.iterator();
  271. while (it.hasNext()) {
  272. NetbeansActionMapping act = it.next();
  273. if (act.getActionName().equals(action.getActionName())) {
  274. int index = lst.indexOf(act);
  275. it.remove();
  276. lst.add(index, action);
  277. return;
  278. }
  279. }
  280. //if not found, add to the end.
  281. lst.add(action);
  282. }
  283. /**
  284. * @since 2.19
  285. */
  286. public static @CheckForNull NetbeansActionMapping getDefaultMapping(String action, Project project) {
  287. return ActionToGoalUtils.getDefaultMapping(action, project);
  288. }
  289. /**
  290. * Load a particular action mapping.
  291. * @param action an action name
  292. * @param project a Maven project
  293. * @param config a configuration of that project
  294. * @return an action mapping model, or null
  295. * @since 2.19
  296. */
  297. public static @CheckForNull NetbeansActionMapping getMapping(String action, Project project, ProjectConfiguration config) {
  298. return ActionToGoalUtils.getActiveMapping(action, project, (M2Configuration) config);
  299. }
  300. /**
  301. * Store a particular action mapping.
  302. * @param mapp an action mapping model
  303. * @param project a Maven project
  304. * @param config a configuration of that project
  305. * @throws IOException in case of trouble
  306. * @since 2.19
  307. */
  308. public static void putMapping(NetbeansActionMapping mapp, Project project, ProjectConfiguration config) throws IOException {
  309. M2Configuration cfg = (M2Configuration) config;
  310. ActionToGoalMapping mapping;
  311. try {
  312. mapping = new NetbeansBuildActionXpp3Reader().read(new StringReader(cfg.getRawMappingsAsString()));
  313. } catch (XmlPullParserException x) {
  314. throw new IOException(x);
  315. }
  316. NetbeansActionMapping existing = null;
  317. for (NetbeansActionMapping m : mapping.getActions()) {
  318. if (m.getActionName().equals(mapp.getActionName())) {
  319. existing = m;
  320. break;
  321. }
  322. }
  323. if (existing != null) {
  324. mapping.getActions().set(mapping.getActions().indexOf(existing), mapp);
  325. } else {
  326. mapping.addAction(mapp);
  327. }
  328. CustomizerProviderImpl.writeNbActionsModel(project, mapping, M2Configuration.getFileNameExt(cfg.getId()));
  329. }
  330. /**
  331. * a javabean wrapper for configurations within the project customizer
  332. *
  333. */
  334. public static class Configuration {
  335. private String id;
  336. private boolean profileBased = false;
  337. private boolean defaul = false;
  338. private List<String> activatedProfiles;
  339. private Map<String, String> properties;
  340. private boolean shared = false;
  341. Configuration() {}
  342. public String getFileNameExt() {
  343. return M2Configuration.getFileNameExt(id);
  344. }
  345. public boolean isDefault() {
  346. return defaul;
  347. }
  348. public void setDefault(boolean def) {
  349. this.defaul = def;
  350. }
  351. public List<String> getActivatedProfiles() {
  352. return activatedProfiles;
  353. }
  354. public void setActivatedProfiles(List<String> activatedProfiles) {
  355. this.activatedProfiles = activatedProfiles;
  356. }
  357. public Map<String, String> getProperties() {
  358. return properties;
  359. }
  360. public void setProperties(Map<String, String> properties) {
  361. this.properties = properties;
  362. }
  363. @Messages({
  364. "DefaultConfig=<default config>",
  365. "# {0} - config ID", "ProfileConfig={0} (Profile)",
  366. "# {0} - config ID", "# {1} - list of profiles", "CustomConfig1={0} (Profiles: {1})",
  367. "# {0} - config ID", "CustomConfig2={0}"
  368. })
  369. public String getDisplayName() {
  370. if (isDefault()) {
  371. return DefaultConfig();
  372. }
  373. if (isProfileBased()) {
  374. return ProfileConfig(id);
  375. }
  376. if (getActivatedProfiles() != null && getActivatedProfiles().size() > 0) {
  377. return CustomConfig1(id, Arrays.toString(getActivatedProfiles().toArray()));
  378. }
  379. return CustomConfig2(id);
  380. }
  381. public String getId() {
  382. return id;
  383. }
  384. public void setShared(boolean shared) {
  385. this.shared = shared;
  386. }
  387. public boolean isShared() {
  388. return shared;
  389. }
  390. void setId(String id) {
  391. this.id = id;
  392. }
  393. public boolean isProfileBased() {
  394. return profileBased;
  395. }
  396. void setProfileBased(boolean profileBased) {
  397. this.profileBased = profileBased;
  398. }
  399. @Override
  400. public String toString() {
  401. return getDisplayName();
  402. }
  403. }
  404. }