~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/j2seplatform/src/org/netbeans/modules/java/j2seplatform/J2SEPlatformModule.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.modules.java.j2seplatform;
 
42
 
 
43
import java.io.IOException;
 
44
import java.util.concurrent.Callable;
 
45
import java.util.logging.Level;
 
46
import java.util.logging.Logger;
 
47
import java.util.logging.Logger;
 
48
import org.openide.cookies.InstanceCookie;
 
49
import org.openide.filesystems.FileObject;
 
50
import org.openide.filesystems.Repository;
 
51
import org.openide.loaders.DataObject;
 
52
import org.openide.modules.ModuleInstall;
 
53
import org.openide.util.Exceptions;
 
54
import org.openide.util.Exceptions;
 
55
import org.netbeans.api.java.platform.JavaPlatform;
 
56
import org.netbeans.api.java.platform.JavaPlatformManager;
 
57
import org.netbeans.api.java.platform.Specification;
 
58
import org.netbeans.api.project.ProjectManager;
 
59
import org.netbeans.spi.project.support.ant.EditableProperties;
 
60
import org.netbeans.spi.project.support.ant.PropertyUtils;
 
61
import org.netbeans.modules.java.j2seplatform.platformdefinition.PlatformConvertor;
 
62
import org.netbeans.modules.java.j2seplatform.platformdefinition.J2SEPlatformImpl;
 
63
 
 
64
 
 
65
public class J2SEPlatformModule extends ModuleInstall {
 
66
    
 
67
    private static final String DEFAULT_PLATFORM = "Services/Platforms/org-netbeans-api-java-Platform/default_platform.xml";    //NOI18N
 
68
 
 
69
    public void restored() {
 
70
        super.restored();
 
71
        // update source level and J2SE platforms in build.properties file
 
72
        updateBuildProperties();
 
73
    }
 
74
 
 
75
    // implemented as separate method for simpler unit testing
 
76
    public static void updateBuildProperties() {
 
77
        ProjectManager.mutex().postWriteRequest(
 
78
            new Runnable () {
 
79
                public void run () {
 
80
                    try {
 
81
                        recoverDefaultPlatform ();
 
82
                        EditableProperties ep = PropertyUtils.getGlobalProperties();
 
83
                        boolean save = updateSourceLevel(ep);
 
84
                        save |= updateBuildProperties (ep);
 
85
                        if (save) {
 
86
                            PropertyUtils.putGlobalProperties (ep);
 
87
                        }
 
88
                    } catch (IOException ioe) {
 
89
                        Exceptions.printStackTrace(ioe);
 
90
                    }
 
91
                }
 
92
            });
 
93
    }
 
94
    
 
95
    private static boolean updateSourceLevel(EditableProperties ep) {
 
96
        JavaPlatform platform = JavaPlatformManager.getDefault().getDefaultPlatform();
 
97
        String ver = platform.getSpecification().getVersion().toString();
 
98
        if (!ver.equals(ep.getProperty("default.javac.source"))) { //NOI18N
 
99
            ep.setProperty("default.javac.source", ver); //NOI18N
 
100
            ep.setProperty("default.javac.target", ver); //NOI18N
 
101
            return true;
 
102
        } else {
 
103
            return false;
 
104
        }
 
105
    }
 
106
 
 
107
 
 
108
    private static boolean updateBuildProperties (EditableProperties ep) {
 
109
        boolean changed = false;
 
110
        JavaPlatform[] installedPlatforms = JavaPlatformManager.getDefault().getPlatforms(null, new Specification ("j2se",null));   //NOI18N
 
111
        for (int i=0; i<installedPlatforms.length; i++) {
 
112
            //Handle only platforms created by this module
 
113
            if (!installedPlatforms[i].equals (JavaPlatformManager.getDefault().getDefaultPlatform()) && installedPlatforms[i] instanceof J2SEPlatformImpl) {
 
114
                String systemName = ((J2SEPlatformImpl)installedPlatforms[i]).getAntName();
 
115
                String key = PlatformConvertor.createName(systemName,"home");   //NOI18N
 
116
                if (!ep.containsKey (key)) {
 
117
                    try {
 
118
                        PlatformConvertor.generatePlatformProperties(installedPlatforms[i], systemName, ep);
 
119
                        changed = true;
 
120
                    } catch (PlatformConvertor.BrokenPlatformException b) {
 
121
                        Logger.getLogger(J2SEPlatformModule.class.getName()).info("Platform: " + installedPlatforms[i].getDisplayName() +" is missing: " + b.getMissingTool());
 
122
                    } catch (IOException ioe) {
 
123
                        Exceptions.printStackTrace(ioe);
 
124
                  }
 
125
                }
 
126
            }
 
127
        }
 
128
        return changed;
 
129
    }
 
130
 
 
131
    private static void recoverDefaultPlatform () {
 
132
        final FileObject defaultPlatform = Repository.getDefault().getDefaultFileSystem().findResource(DEFAULT_PLATFORM);
 
133
        if (defaultPlatform != null) {
 
134
            try {
 
135
                DataObject dobj = DataObject.find(defaultPlatform);
 
136
                boolean valid = false;
 
137
                InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
 
138
                if (ic != null) {
 
139
                    try {
 
140
                        ic.instanceCreate();
 
141
                        valid = true;
 
142
                    } catch (Exception e) {
 
143
                        //Ignore it, logged bellow
 
144
                    }
 
145
                }
 
146
                if (!valid) {
 
147
                    Logger.getLogger("global").log(Level.WARNING,"default_platform.xml is broken, regenerating.");
 
148
                    Object attr = defaultPlatform.getAttribute("removeWritables");      //NOI18N
 
149
                    if (attr instanceof Callable) {
 
150
                        ((Callable)attr).call ();
 
151
                    }
 
152
                }
 
153
            } catch (Exception e) {
 
154
                Exceptions.printStackTrace(e);
 
155
            }
 
156
        }
 
157
        else {
 
158
            Logger.getLogger("global").log(Level.WARNING,"The default platform is hidden.");  //NOI18N
 
159
        }
 
160
    }
 
161
}