This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 201340
Collapse All | Expand All

(-)a/whitelist/apichanges.xml (+133 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!--
3
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
5
Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
6
7
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
8
Other names may be trademarks of their respective owners.
9
10
11
The contents of this file are subject to the terms of either the GNU
12
General Public License Version 2 only ("GPL") or the Common
13
Development and Distribution License("CDDL") (collectively, the
14
"License"). You may not use this file except in compliance with the
15
License. You can obtain a copy of the License at
16
http://www.netbeans.org/cddl-gplv2.html
17
or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
18
specific language governing permissions and limitations under the
19
License.  When distributing the software, include this License Header
20
Notice in each file and include the License file at
21
nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
22
particular file as subject to the "Classpath" exception as provided
23
by Oracle in the GPL Version 2 section of the License file that
24
accompanied this code. If applicable, add the following below the
25
License Header, with the fields enclosed by brackets [] replaced by
26
your own identifying information:
27
"Portions Copyrighted [year] [name of copyright owner]"
28
29
Contributor(s):
30
31
The Original Software is NetBeans. The Initial Developer of the Original
32
Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
33
Microsystems, Inc. All Rights Reserved.
34
35
If you wish your version of this file to be governed by only the CDDL
36
or only the GPL Version 2, indicate your decision by adding
37
"[Contributor] elects to include this software in this distribution
38
under the [CDDL or GPL Version 2] license." If you do not indicate a
39
single choice of license, a recipient has the option to distribute
40
your version of this file under either the CDDL, the GPL Version 2 or
41
to extend the choice of license to its licensees as provided above.
42
However, if you add GPL Version 2 code and therefore, elected the GPL
43
Version 2 license, then the option applies only if the new code is
44
made subject to such option by the copyright holder.
45
-->
46
47
<?xml-stylesheet href="../nbbuild/javadoctools/apichanges.xsl" type="text/xsl"?>
48
<!DOCTYPE apichanges PUBLIC "-//NetBeans//DTD API changes list 1.0//EN" "../nbbuild/javadoctools/apichanges.dtd">
49
50
<!-- INFO FOR PEOPLE ADDING CHANGES:
51
[most of contents snipped - see openide's apichanges for how-to instructions]
52
<change>
53
    <api name="compiler"/>
54
    <summary>Some brief description here, can use <b>XHTML</b></summary>
55
    <version major="1" minor="99"/>
56
    <date day="13" month="6" year="2001"/>
57
    <author login="jrhacker"/>
58
    <compatibility addition="yes"/>
59
    <description>
60
        The main description of the change here.
61
        Again can use full <b>XHTML</b> as needed.
62
    </description>
63
    <class package="org.openide.compiler" name="DoWhatIWantCompiler"/>
64
    <issue number="14309"/>
65
</change>
66
-->
67
68
<apichanges>
69
70
<!-- First, a list of API names you may use: -->
71
<apidefs>
72
        <apidef name="whitelist">White List API</apidef>
73
</apidefs>
74
75
<!-- ACTUAL CHANGES BEGIN HERE: -->
76
77
<changes>
78
        <change id="WhiteListQuery">
79
            <api name="whitelist"/>
80
            <summary>Added WhiteListQuery to check violations of runtime restrictions</summary>
81
            <version major="1" minor="0"/>
82
            <date day="26" month="8" year="2011"/>
83
            <author login="tzezula"/>
84
            <compatibility addition="yes" modification="no" semantic="compatible" source="compatible" binary="compatible"/>
85
            <description>
86
                <p>
87
                    Added WhiteListQuery to check violations of runtime restrictions. The project types supporting a runtime
88
                    with class usage restriction implement the <code>WhiteListQueryImplementation</code> to provide a white list
89
                    of allowed types (methods).
90
                </p>
91
            </description>
92
            <class package="org.netbeans.api.whitelist" name="WhiteListQuery"/>
93
            <class package="org.netbeans.spi.whitelist" name="WhiteListQueryImplementation"/>
94
            <class package="org.netbeans.spi.whitelist.support" name="WhiteListQueryMergerSupport"/>
95
        </change>
96
</changes>
97
98
  <!-- Now the surrounding HTML text and document structure: -->
99
100
  <htmlcontents>
101
<!--
102
103
                            NO NO NO NO NO!
104
105
         ==============>    DO NOT EDIT ME!  <======================
106
107
          AUTOMATICALLY GENERATED FROM APICHANGES.XML, DO NOT EDIT
108
109
                SEE xml/api/doc/changes/apichanges.xml
110
111
-->
112
    <head>
113
      <title>White List API changes by date</title>
114
      <link rel="stylesheet" href="prose.css" type="text/css"/>
115
    </head>
116
    <body>
117
118
<p class="overviewlink"><a href="overview-summary.html">Overview</a></p>
119
120
<h1>Introduction</h1>
121
122
<p>This document lists changes made to the White List  APIs. Please ask on the
123
<code>dev@java.netbeans.org</code> or <code>nbdev@netbeans.org</code>
124
mailing list if you have any questions about the details of a
125
change, or are wondering how to convert existing code to be compatible.</p>
126
127
      <hr/><standard-changelists module-code-name="org.netbeans.modules.whitelist"/>
128
129
      <hr/><p>@FOOTER@</p>
130
131
    </body>
132
  </htmlcontents>
133
</apichanges>
(-)a/whitelist/build.xml (+5 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project basedir="." default="netbeans" name="whitelist">
3
    <description>Builds, tests, and runs the project org.netbeans.modules.whitelist</description>
4
    <import file="../nbbuild/templates/projectized.xml"/>
5
</project>
(-)a/whitelist/manifest.mf (+6 lines)
Line 0 Link Here
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.whitelist
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/whitelist/Bundle.properties
4
OpenIDE-Module-Layer: org/netbeans/modules/whitelist/resources/layer.xml
5
OpenIDE-Module-Specification-Version: 1.0
6
(-)a/whitelist/nbproject/project.properties (+5 lines)
Line 0 Link Here
1
is.autoload=true
2
javac.source=1.6
3
javac.compilerargs=-Xlint -Xlint:-serial
4
javadoc.apichanges=${basedir}/apichanges.xml
5
(-)a/whitelist/nbproject/project.xml (+86 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://www.netbeans.org/ns/project/1">
3
    <type>org.netbeans.modules.apisupport.project</type>
4
    <configuration>
5
        <data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
6
            <code-name-base>org.netbeans.modules.whitelist</code-name-base>
7
            <module-dependencies>
8
                <dependency>
9
                    <code-name-base>org.netbeans.api.annotations.common</code-name-base>
10
                    <build-prerequisite/>
11
                    <compile-dependency/>
12
                    <run-dependency>
13
                        <release-version>1</release-version>
14
                        <specification-version>1.10</specification-version>
15
                    </run-dependency>
16
                </dependency>
17
                <dependency>
18
                    <code-name-base>org.netbeans.libs.javacapi</code-name-base>
19
                    <build-prerequisite/>
20
                    <compile-dependency/>
21
                    <run-dependency>
22
                        <specification-version>7.7</specification-version>
23
                    </run-dependency>
24
                </dependency>
25
                <dependency>
26
                    <code-name-base>org.netbeans.modules.java.source</code-name-base>
27
                    <build-prerequisite/>
28
                    <compile-dependency/>
29
                    <run-dependency>
30
                        <specification-version>0.84</specification-version>
31
                    </run-dependency>
32
                </dependency>
33
                <dependency>
34
                    <code-name-base>org.netbeans.modules.projectapi</code-name-base>
35
                    <build-prerequisite/>
36
                    <compile-dependency/>
37
                    <run-dependency>
38
                        <release-version>1</release-version>
39
                        <specification-version>1.37</specification-version>
40
                    </run-dependency>
41
                </dependency>
42
                <dependency>
43
                    <code-name-base>org.netbeans.modules.projectuiapi</code-name-base>
44
                    <build-prerequisite/>
45
                    <compile-dependency/>
46
                    <run-dependency>
47
                        <release-version>1</release-version>
48
                        <specification-version>1.52</specification-version>
49
                    </run-dependency>
50
                </dependency>
51
                <dependency>
52
                    <code-name-base>org.openide.filesystems</code-name-base>
53
                    <build-prerequisite/>
54
                    <compile-dependency/>
55
                    <run-dependency>
56
                        <specification-version>7.49</specification-version>
57
                    </run-dependency>
58
                </dependency>
59
                <dependency>
60
                    <code-name-base>org.openide.util</code-name-base>
61
                    <build-prerequisite/>
62
                    <compile-dependency/>
63
                    <run-dependency>
64
                        <specification-version>8.16</specification-version>
65
                    </run-dependency>
66
                </dependency>
67
                <dependency>
68
                    <code-name-base>org.openide.util.lookup</code-name-base>
69
                    <build-prerequisite/>
70
                    <compile-dependency/>
71
                    <run-dependency>
72
                        <specification-version>8.10</specification-version>
73
                    </run-dependency>
74
                </dependency>
75
            </module-dependencies>
76
            <friend-packages>
77
                <friend>org.netbeans.modules.j2ee.weblogic9</friend>
78
                <friend>org.netbeans.modules.java.editor</friend>
79
                <friend>org.netbeans.modules.web.project</friend>
80
                <package>org.netbeans.api.whitelist</package>
81
                <package>org.netbeans.spi.whitelist</package>
82
                <package>org.netbeans.spi.whitelist.support</package>
83
            </friend-packages>
84
        </data>
85
    </configuration>
86
</project>
(-)a/whitelist/src/org/netbeans/api/whitelist/WhiteListQuery.java (+200 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.api.whitelist;
43
44
import javax.swing.event.ChangeListener;
45
import org.netbeans.api.annotations.common.CheckForNull;
46
import org.netbeans.api.annotations.common.NonNull;
47
import org.netbeans.api.java.source.ElementHandle;
48
import org.netbeans.api.project.Project;
49
import org.netbeans.modules.whitelist.project.WhiteListCategoryPanel;
50
import org.netbeans.spi.whitelist.WhiteListQueryImplementation;
51
import org.netbeans.spi.whitelist.WhiteListQueryImplementation.WhiteListImplementation;
52
import org.openide.filesystems.FileObject;
53
import org.openide.util.Lookup;
54
import org.openide.util.Parameters;
55
56
/**
57
 * Query for finding white lists used to emit errors for usages of non allowed
58
 * types or methods. The project types supporting a runtime with class usage
59
 * restriction implement the {@link WhiteListQueryImplementation} to provide a
60
 * white list of allowed types (methods).
61
 * @author Tomas Zezula
62
 * @author David Konecny
63
 */
64
public final class WhiteListQuery {
65
66
    private static final Lookup.Result<? extends WhiteListQueryImplementation> queries =
67
        Lookup.getDefault().lookupResult(WhiteListQueryImplementation.class);
68
69
    private WhiteListQuery(){}
70
71
    /**
72
     * Returns a white list for given file.
73
     * @param file the file for which the white list should be obtained.
74
     * @return the {@link WhiteList} for given file or null if no white list
75
     * is associated with given file.
76
     */
77
    @CheckForNull
78
    public static WhiteList getWhiteList(@NonNull final FileObject file) {
79
        Parameters.notNull("file", file);   //NOI18N
80
        for (WhiteListQueryImplementation query : queries.allInstances()) {
81
            final WhiteListImplementation whiteListImpl = query.getWhiteList(file);
82
            if (whiteListImpl != null) {
83
                return new WhiteList(whiteListImpl);
84
            }
85
        }
86
        return null;
87
    }
88
89
    /**
90
     * Enables the given white list in the project.
91
     * At the moment assumption is that if a project want to automatically enable
92
     * some white list then they likely know whitelistId.
93
     * @param project the project in  which the white list should be enabled
94
     * @param whiteListId the white list identifier
95
     * @param enable if true the white list is enabled if false white list is disabled
96
     */
97
    public static void enableWhiteListInProject(@NonNull Project project, @NonNull String whiteListId, boolean enable) {
98
        WhiteListCategoryPanel.enableWhiteListInProject(project, whiteListId, enable);
99
    }
100
101
    /**
102
     * The white list used to emit errors for usages of non allowed
103
     * types or methods.
104
     */
105
    public static final class WhiteList {
106
107
        private final WhiteListImplementation impl;
108
109
        private WhiteList(
110
            @NonNull final WhiteListImplementation impl) {
111
            Parameters.notNull("impl", impl);   //NOI18N
112
            this.impl = impl;
113
        }
114
115
        /**
116
         * Checks if given method (type) can be invoked (accessed).
117
         * @param element to check
118
         * @param operation the operation which should be tested
119
         * @return a {@link Result} holding the details.
120
         */
121
        public final Result check(
122
            @NonNull final ElementHandle<?> element,
123
            @NonNull final Operation operation) {
124
            Parameters.notNull("element", element); //NOI18N;
125
            Parameters.notNull("operation", operation); //NOI18N;
126
            return impl.check(element, operation);
127
        }
128
129
        /**
130
         * Adds {@link ChangeListener} to white list. The listener is
131
         * notified when the white list is changed.
132
         * @param listener to be added
133
         */
134
        public void addChangeListener(@NonNull final ChangeListener listener) {
135
            Parameters.notNull("listener", listener);   //NOI18N
136
            impl.addChangeListener(listener);
137
        }
138
139
        /**
140
         * Removes {@link ChangeListener} from white list.
141
         * @param listener to be removed
142
         */
143
        public void removeChangeListener(@NonNull final ChangeListener listener) {
144
            Parameters.notNull("listener", listener);   //NOI18N
145
            impl.removeChangeListener(listener);
146
        }
147
    }
148
149
    /**
150
     * Operation on element to be tested (usage, subclassing)
151
     */
152
    public enum Operation {
153
        /**
154
         * Checks the white list if usage of given element is supported.
155
         */
156
        USAGE
157
    }
158
159
    /**
160
     * Result of the white list check.
161
     */
162
    public static final class Result {
163
        private final boolean allowed;
164
        private final String violatedRuleName;
165
        private final String violatedRuleDescription;
166
167
        //Todo: Make private
168
        public Result(boolean allowed, String violatedRuleName, String violatedRuleDescription) {
169
            this.allowed = allowed;
170
            this.violatedRuleName = violatedRuleName;
171
            this.violatedRuleDescription = violatedRuleDescription;
172
        }
173
174
        /**
175
         * Returns true if the operation on given element is allowed by white list.
176
         * @return true if the operation on given element is allowed
177
         */
178
        public boolean isAllowed() {
179
            return allowed;
180
        }
181
182
        /**
183
         * Returns description of violated rule if {@link WhiteListQuery.Result#isAllowed}
184
         * returned false otherwise returns null.
185
         * @return the description of violated rule.
186
         */
187
        public String getViolatedRuleDescription() {
188
            return violatedRuleDescription;
189
        }
190
191
        /**
192
         * Returns name of violated rule if {@link WhiteListQuery.Result#isAllowed}
193
         * returned false otherwise returns null.
194
         * @return the name of violated rule.
195
         */
196
        public String getViolatedRuleName() {
197
            return violatedRuleName;
198
        }
199
    }
200
}
(-)a/whitelist/src/org/netbeans/modules/whitelist/Bundle.properties (+2 lines)
Line 0 Link Here
1
OpenIDE-Module-Display-Category=Java
2
OpenIDE-Module-Name=Whitelist Support
(-)a/whitelist/src/org/netbeans/modules/whitelist/ProjectWhiteListQueryImpl.java (+67 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.whitelist;
43
44
import org.netbeans.api.project.FileOwnerQuery;
45
import org.netbeans.api.project.Project;
46
import org.netbeans.spi.whitelist.WhiteListQueryImplementation;
47
import org.openide.filesystems.FileObject;
48
import org.openide.util.lookup.ServiceProvider;
49
50
/**
51
 *
52
 * @author Tomas Zezula
53
 */
54
@ServiceProvider(service=WhiteListQueryImplementation.class, position=100)
55
public class ProjectWhiteListQueryImpl implements WhiteListQueryImplementation {
56
    @Override
57
    public WhiteListImplementation getWhiteList(FileObject file) {
58
        final Project project = FileOwnerQuery.getOwner(file);
59
        if (project != null) {
60
            final WhiteListQueryImplementation delegate = project.getLookup().lookup(WhiteListQueryImplementation.class);
61
            if (delegate != null) {
62
                return delegate.getWhiteList(file);
63
            }
64
        }
65
        return null;
66
    }
67
}
(-)a/whitelist/src/org/netbeans/modules/whitelist/project/Bundle.properties (+3 lines)
Line 0 Link Here
1
WhiteListCategoryPanel.jLabel1.text=Active whitelists:
2
LBL_CategoryWhitelist=Whitelists
3
WhiteListCategoryPanel.jLabel2.text=<html>Note: This project has been used in context of enteprise application which runtime container is constrained by above Whitelists.
(-)a/whitelist/src/org/netbeans/modules/whitelist/project/WhiteListCategoryPanel.form (+66 lines)
Line 0 Link Here
1
<?xml version="1.1" encoding="UTF-8" ?>
2
3
<Form version="1.5" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
4
  <AuxValues>
5
    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="1"/>
6
    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
7
    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
8
    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
9
    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="true"/>
10
    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
11
    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
12
    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
13
    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
14
  </AuxValues>
15
16
  <Layout>
17
    <DimensionLayout dim="0">
18
      <Group type="103" groupAlignment="0" attributes="0">
19
          <Group type="102" attributes="0">
20
              <Component id="jLabel1" min="-2" max="-2" attributes="0"/>
21
              <EmptySpace pref="283" max="32767" attributes="0"/>
22
          </Group>
23
          <Component id="jLabel2" alignment="0" pref="400" max="32767" attributes="0"/>
24
          <Component id="jScrollPane1" alignment="0" pref="400" max="32767" attributes="0"/>
25
      </Group>
26
    </DimensionLayout>
27
    <DimensionLayout dim="1">
28
      <Group type="103" groupAlignment="0" attributes="0">
29
          <Group type="102" alignment="0" attributes="0">
30
              <Component id="jLabel1" min="-2" max="-2" attributes="0"/>
31
              <EmptySpace max="-2" attributes="0"/>
32
              <Component id="jScrollPane1" pref="233" max="32767" attributes="0"/>
33
              <EmptySpace type="unrelated" max="-2" attributes="0"/>
34
              <Component id="jLabel2" min="-2" max="-2" attributes="0"/>
35
          </Group>
36
      </Group>
37
    </DimensionLayout>
38
  </Layout>
39
  <SubComponents>
40
    <Component class="javax.swing.JLabel" name="jLabel1">
41
      <Properties>
42
        <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
43
          <ResourceString bundle="org/netbeans/modules/whitelist/project/Bundle.properties" key="WhiteListCategoryPanel.jLabel1.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
44
        </Property>
45
      </Properties>
46
    </Component>
47
    <Container class="javax.swing.JScrollPane" name="jScrollPane1">
48
      <AuxValues>
49
        <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
50
      </AuxValues>
51
52
      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
53
      <SubComponents>
54
        <Component class="javax.swing.JTable" name="jTable1">
55
        </Component>
56
      </SubComponents>
57
    </Container>
58
    <Component class="javax.swing.JLabel" name="jLabel2">
59
      <Properties>
60
        <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
61
          <ResourceString bundle="org/netbeans/modules/whitelist/project/Bundle.properties" key="WhiteListCategoryPanel.jLabel2.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
62
        </Property>
63
      </Properties>
64
    </Component>
65
  </SubComponents>
66
</Form>
(-)a/whitelist/src/org/netbeans/modules/whitelist/project/WhiteListCategoryPanel.java (+336 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
43
/*
44
 * WhiteListCategoryPanel.java
45
 *
46
 * Created on 22/07/2011, 7:37:04 AM
47
 */
48
package org.netbeans.modules.whitelist.project;
49
50
import java.awt.Dimension;
51
import java.awt.event.ActionEvent;
52
import java.awt.event.ActionListener;
53
import java.lang.ref.Reference;
54
import java.lang.ref.WeakReference;
55
import java.util.ArrayList;
56
import java.util.Collection;
57
import java.util.List;
58
import java.util.Map;
59
import java.util.WeakHashMap;
60
import java.util.prefs.Preferences;
61
import javax.swing.DefaultCellEditor;
62
import javax.swing.JCheckBox;
63
import javax.swing.JComponent;
64
import javax.swing.event.TableModelListener;
65
import javax.swing.table.TableModel;
66
import org.netbeans.api.annotations.common.NonNull;
67
import org.netbeans.api.project.Project;
68
import org.netbeans.api.project.ProjectManager;
69
import org.netbeans.api.project.ProjectUtils;
70
import org.netbeans.api.whitelist.WhiteListQuery;
71
import org.netbeans.spi.project.ui.support.ProjectCustomizer;
72
import org.netbeans.spi.whitelist.WhiteListQueryImplementation;
73
import org.netbeans.spi.whitelist.WhiteListQueryImplementation.UserSelectable;
74
import org.openide.util.Lookup;
75
import org.openide.util.NbBundle;
76
import org.openide.util.lookup.Lookups;
77
import org.openide.util.lookup.ProxyLookup;
78
79
/**
80
 *
81
 * @author david
82
 */
83
public class WhiteListCategoryPanel extends javax.swing.JPanel implements ActionListener {
84
85
    private static final String WHITELISTS_PATH = "org-netbeans-api-java/whitelists/";  //NOI18N
86
87
    private static Map<Project,Reference<WhiteListLookup>> lookupCache =
88
            new WeakHashMap<Project, Reference<WhiteListLookup>>();
89
90
    private Project p;
91
92
    /** Creates new form WhiteListCategoryPanel */
93
    public WhiteListCategoryPanel(Project p) {
94
        this.p = p;
95
        initComponents();
96
        WhiteListsModel model = new WhiteListsModel(getTableContent());
97
        jTable1.setModel(model);
98
        jTable1.getTableHeader().setVisible(false);
99
        jTable1.getTableHeader().setPreferredSize(new Dimension(0, 0));
100
        jTable1.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JCheckBox()));
101
        jTable1.getColumnModel().getColumn(0).setMaxWidth(25);
102
        jTable1.getColumnModel().getColumn(0).setMinWidth(25);
103
    }
104
105
    private List<Desc> getTableContent() {
106
        List<Desc> l = new ArrayList<Desc>();
107
        for (WhiteListQueryImplementation.UserSelectable impl : getUserSelectableWhiteLists()) {
108
            l.add(new Desc(impl, isWhiteListEnabledInProject(p, impl.getId())));
109
        }
110
        return l;
111
    }
112
    
113
    public static ProjectCustomizer.CompositeCategoryProvider createWhiteListCategoryProvider(Map attrs) {
114
        return new Factory(); //NOI18N
115
    }
116
117
    @Override
118
    public void actionPerformed(ActionEvent e) {
119
        for (Desc d : ((WhiteListsModel)jTable1.getModel()).whitelists) {
120
            enableWhiteListInProject(p, d.w.getId(), d.active);
121
        }
122
    }
123
124
    public static class Factory implements ProjectCustomizer.CompositeCategoryProvider {
125
 
126
        private static final String CATEGORY_WHITELIST = "WhiteList"; // NOI18N
127
128
        public Factory() {
129
        }
130
131
        public ProjectCustomizer.Category createCategory(Lookup context) {
132
            Project p = context.lookup(Project.class);
133
            if (p == null) {
134
                return null;
135
            }
136
            if (!isWhiteListPanelEnabled(p) || getUserSelectableWhiteLists().size() == 0) {
137
                return null;
138
            }
139
            return ProjectCustomizer.Category.create(
140
                    CATEGORY_WHITELIST, 
141
                    NbBundle.getMessage(WhiteListCategoryPanel.class, "LBL_CategoryWhitelist"), //NOI18N
142
                    null);
143
        }
144
145
        public JComponent createComponent(ProjectCustomizer.Category category, Lookup context) {
146
            Project p = context.lookup(Project.class);
147
            assert p != null;
148
            WhiteListCategoryPanel customizerPanel = new WhiteListCategoryPanel(p);
149
            category.setStoreListener(customizerPanel);
150
            return customizerPanel;
151
        }
152
    } // End of Factory class
153
    
154
    public static final String PROP_WHITELIST_ENABLED = "whitelist-enabled";
155
    public static final String PROP_WHITELIST = "whitelist-";
156
    
157
    public static boolean isWhiteListPanelEnabled(@NonNull Project p) {
158
        Preferences prefs = ProjectUtils.getPreferences(p, WhiteListQuery.class, true);
159
        return prefs.getBoolean(PROP_WHITELIST_ENABLED, false);
160
    }
161
    
162
    public static boolean isWhiteListEnabledInProject(@NonNull Project p, @NonNull String whiteListId) {
163
        Preferences prefs = ProjectUtils.getPreferences(p, WhiteListQuery.class, true);
164
        return prefs.getBoolean(PROP_WHITELIST+whiteListId, false);
165
    }
166
    
167
    public static void enableWhiteListInProject(@NonNull Project p, final @NonNull String whiteListId, final boolean enable) {
168
        final Preferences prefs = ProjectUtils.getPreferences(p, WhiteListQuery.class, true);
169
        ProjectManager.mutex().writeAccess(new Runnable() {
170
            @Override
171
            public void run() {
172
                prefs.putBoolean(PROP_WHITELIST+whiteListId, enable);
173
                 if (enable) {
174
                    prefs.putBoolean(PROP_WHITELIST_ENABLED, true);
175
                }
176
            }
177
        });
178
        synchronized (WhiteListCategoryPanel.class) {
179
            final Reference<WhiteListLookup> lkpRef = lookupCache.get(p);
180
            final WhiteListLookup lkp;
181
            if (lkpRef != null && (lkp=lkpRef.get())!=null) {
182
                lkp.updateLookup(p);
183
            }
184
        }
185
    }
186
187
    public static Collection<? extends WhiteListQueryImplementation.UserSelectable> getUserSelectableWhiteLists() {
188
        return Lookups.forPath(WHITELISTS_PATH).lookupResult(WhiteListQueryImplementation.UserSelectable.class).allInstances();
189
    }
190
191
    public static Lookup getEnabledUserSelectableWhiteLists(@NonNull Project p) {
192
        Reference<WhiteListLookup> lkpRef = lookupCache.get(p);
193
        WhiteListLookup lkp;
194
        if (lkpRef == null || (lkp=lkpRef.get())==null) {
195
            lkp = new WhiteListLookup();
196
            lkp.updateLookup(p);
197
            lookupCache.put(p,new WeakReference<WhiteListLookup>(lkp));
198
        }
199
        return lkp;
200
    }
201
202
    /** This method is called from within the constructor to
203
     * initialize the form.
204
     * WARNING: Do NOT modify this code. The content of this method is
205
     * always regenerated by the Form Editor.
206
     */
207
    @SuppressWarnings("unchecked")
208
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
209
    private void initComponents() {
210
211
        jLabel1 = new javax.swing.JLabel();
212
        jScrollPane1 = new javax.swing.JScrollPane();
213
        jTable1 = new javax.swing.JTable();
214
        jLabel2 = new javax.swing.JLabel();
215
216
        jLabel1.setText(org.openide.util.NbBundle.getMessage(WhiteListCategoryPanel.class, "WhiteListCategoryPanel.jLabel1.text")); // NOI18N
217
218
        jScrollPane1.setViewportView(jTable1);
219
220
        jLabel2.setText(org.openide.util.NbBundle.getMessage(WhiteListCategoryPanel.class, "WhiteListCategoryPanel.jLabel2.text")); // NOI18N
221
222
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
223
        this.setLayout(layout);
224
        layout.setHorizontalGroup(
225
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
226
            .addGroup(layout.createSequentialGroup()
227
                .addComponent(jLabel1)
228
                .addContainerGap(283, Short.MAX_VALUE))
229
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
230
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
231
        );
232
        layout.setVerticalGroup(
233
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
234
            .addGroup(layout.createSequentialGroup()
235
                .addComponent(jLabel1)
236
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
237
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
238
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
239
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
240
        );
241
    }// </editor-fold>//GEN-END:initComponents
242
    // Variables declaration - do not modify//GEN-BEGIN:variables
243
    private javax.swing.JLabel jLabel1;
244
    private javax.swing.JLabel jLabel2;
245
    private javax.swing.JScrollPane jScrollPane1;
246
    private javax.swing.JTable jTable1;
247
    // End of variables declaration//GEN-END:variables
248
249
    private static class Desc {
250
        private WhiteListQueryImplementation.UserSelectable w;
251
        private boolean active;
252
253
        public Desc(UserSelectable w, boolean active) {
254
            this.w = w;
255
            this.active = active;
256
        }
257
    }
258
    
259
    private static class WhiteListsModel implements TableModel {
260
261
        private List<Desc> whitelists;
262
263
        public WhiteListsModel(List<Desc> whitelists) {
264
            assert whitelists.size() > 0;
265
            this.whitelists = new ArrayList(whitelists);
266
        }
267
        
268
        @Override
269
        public int getRowCount() {
270
            return whitelists.size();
271
        }
272
273
        @Override
274
        public int getColumnCount() {
275
            return 2;
276
        }
277
278
        private String[] header = new String[]{"Enabled", "Whitelist"};
279
        private Class[] headerClass = new Class[]{Boolean.class, String.class};
280
        
281
        @Override
282
        public String getColumnName(int columnIndex) {
283
            return header[columnIndex];
284
        }
285
286
        @Override
287
        public Class<?> getColumnClass(int columnIndex) {
288
            return headerClass[columnIndex];
289
        }
290
291
        @Override
292
        public boolean isCellEditable(int rowIndex, int columnIndex) {
293
            return columnIndex == 0;
294
        }
295
296
        @Override
297
        public Object getValueAt(int rowIndex, int columnIndex) {
298
            Desc srd = whitelists.get(rowIndex);
299
            if (columnIndex == 0) {
300
                return Boolean.valueOf(srd.active);
301
            } else {
302
                return srd.w.getDisplayName();
303
            }
304
        }
305
306
        @Override
307
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
308
            if (aValue instanceof Boolean) {
309
                whitelists.get(rowIndex).active = ((Boolean)aValue).booleanValue();
310
            }
311
        }
312
313
        @Override
314
        public void addTableModelListener(TableModelListener l) {
315
        }
316
317
        @Override
318
        public void removeTableModelListener(TableModelListener l) {
319
        }
320
        
321
    }
322
323
    private static class WhiteListLookup extends ProxyLookup {
324
325
        public void updateLookup(final Project p) {
326
            final List<WhiteListQueryImplementation.UserSelectable> impls = new ArrayList<WhiteListQueryImplementation.UserSelectable>();
327
            for (WhiteListQueryImplementation.UserSelectable w :
328
                    Lookups.forPath(WHITELISTS_PATH).lookupAll(WhiteListQueryImplementation.UserSelectable.class)) {
329
                if (WhiteListCategoryPanel.isWhiteListEnabledInProject(p, w.getId())) {
330
                    impls.add(w);
331
                }
332
            }
333
            setLookups(Lookups.fixed((UserSelectable[])impls.toArray(new UserSelectable[impls.size()])));
334
        }
335
    }
336
}
(-)a/whitelist/src/org/netbeans/modules/whitelist/project/WhiteListLookupProvider.java (+60 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.whitelist.project;
43
44
import org.netbeans.api.project.Project;
45
import org.netbeans.spi.project.LookupProvider;
46
import org.openide.util.Lookup;
47
48
/**
49
 *
50
 */
51
public class WhiteListLookupProvider implements LookupProvider {
52
53
54
    @Override
55
    public Lookup createAdditionalLookup(Lookup baseContext) {
56
        Project p = baseContext.lookup(Project.class);
57
        assert p != null;
58
        return WhiteListCategoryPanel.getEnabledUserSelectableWhiteLists(p);
59
    }
60
}
(-)a/whitelist/src/org/netbeans/modules/whitelist/resources/layer.xml (+83 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!--
3
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
5
Copyright 1997-2011 Oracle and/or its affiliates. All rights reserved.
6
7
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
8
Other names may be trademarks of their respective owners.
9
10
11
The contents of this file are subject to the terms of either the GNU
12
General Public License Version 2 only ("GPL") or the Common
13
Development and Distribution License("CDDL") (collectively, the
14
"License"). You may not use this file except in compliance with the
15
License. You can obtain a copy of the License at
16
http://www.netbeans.org/cddl-gplv2.html
17
or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
18
specific language governing permissions and limitations under the
19
License.  When distributing the software, include this License Header
20
Notice in each file and include the License file at
21
nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
22
particular file as subject to the "Classpath" exception as provided
23
by Oracle in the GPL Version 2 section of the License file that
24
accompanied this code. If applicable, add the following below the
25
License Header, with the fields enclosed by brackets [] replaced by
26
your own identifying information:
27
"Portions Copyrighted [year] [name of copyright owner]"
28
29
Contributor(s):
30
31
The Original Software is NetBeans. The Initial Developer of the Original
32
Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
33
Microsystems, Inc. All Rights Reserved.
34
35
If you wish your version of this file to be governed by only the CDDL
36
or only the GPL Version 2, indicate your decision by adding
37
"[Contributor] elects to include this software in this distribution
38
under the [CDDL or GPL Version 2] license." If you do not indicate a
39
single choice of license, a recipient has the option to distribute
40
your version of this file under either the CDDL, the GPL Version 2 or
41
to extend the choice of license to its licensees as provided above.
42
However, if you add GPL Version 2 code and therefore, elected the GPL
43
Version 2 license, then the option applies only if the new code is
44
made subject to such option by the copyright holder.
45
-->
46
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
47
<filesystem>
48
    <folder name="org-netbeans-api-java">
49
        <folder name="whitelists"/>
50
    </folder>
51
    <folder name="Projects">
52
        <folder name="org-netbeans-modules-web-project">
53
            <folder name="Customizer">
54
                <file name="Whitelisting.instance">
55
                    <attr name="instanceOf" stringvalue="org.netbeans.spi.project.ui.support.ProjectCustomizer$CompositeCategoryProvider"/>
56
                    <attr name="instanceCreate" methodvalue="org.netbeans.modules.whitelist.project.WhiteListCategoryPanel.createWhiteListCategoryProvider"/>
57
                    <attr name="position" intvalue="115"/>
58
                </file>
59
            </folder>
60
            <folder name="Lookup">
61
                <file name="WhitelistingProvider.instance">
62
                    <attr name="instanceOf" stringvalue="org.netbeans.spi.project.LookupProvider"/>
63
                    <attr name="instanceCreate" newvalue="org.netbeans.modules.whitelist.project.WhiteListLookupProvider"/>
64
                </file>
65
            </folder>
66
        </folder>
67
        <folder name="org-netbeans-modules-java-j2seproject">
68
            <folder name="Customizer">
69
                <file name="Whitelisting.instance">
70
                    <attr name="instanceOf" stringvalue="org.netbeans.spi.project.ui.support.ProjectCustomizer$CompositeCategoryProvider"/>
71
                    <attr name="instanceCreate" methodvalue="org.netbeans.modules.whitelist.project.WhiteListCategoryPanel.createWhiteListCategoryProvider"/>
72
                    <attr name="position" intvalue="115"/>
73
                </file>
74
            </folder>
75
            <folder name="Lookup">
76
                <file name="WhitelistingProvider.instance">
77
                    <attr name="instanceOf" stringvalue="org.netbeans.spi.project.LookupProvider"/>
78
                    <attr name="instanceCreate" newvalue="org.netbeans.modules.whitelist.project.WhiteListLookupProvider"/>
79
                </file>
80
            </folder>
81
        </folder>
82
    </folder>
83
</filesystem>
(-)a/whitelist/src/org/netbeans/spi/whitelist/WhiteListQueryImplementation.java (+104 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.spi.whitelist;
43
44
import javax.swing.event.ChangeListener;
45
import org.netbeans.api.annotations.common.NonNull;
46
import org.netbeans.api.java.source.ElementHandle;
47
import org.netbeans.api.whitelist.WhiteListQuery;
48
import org.openide.filesystems.FileObject;
49
50
/**
51
 * Permits projects to provide wite list used to emit errors for usages of non allowed
52
 * types or methods. The project types supporting a runtime with class usage
53
 * restriction implement this interface to provide a white list of allowed types (methods).
54
 * @author Tomas Zezula
55
 * @author David Konecny
56
 */
57
public interface WhiteListQueryImplementation {
58
59
    /**
60
     * Returns a white list for given file.
61
     * @param file to return white list for.
62
     * @return the {@link WhiteListImplementation} for given file.
63
     */
64
    WhiteListImplementation getWhiteList(
65
          @NonNull FileObject file);
66
67
    /**
68
     * All white lists which should be displayed in UI for user should implement
69
     * this interface. And on the other hand non-visible implementations like
70
     * for example merging WhiteListQueryImplementation should not implement it.
71
     */
72
    public interface UserSelectable extends WhiteListQueryImplementation {
73
        String getDisplayName();
74
        String getId();
75
    }
76
77
    /**
78
     * The white list implementation used to emit errors for usages of non allowed
79
     * types or methods.
80
     */
81
    public interface WhiteListImplementation {
82
83
        /**
84
         * Checks if given method (type) can be invoked (accessed).
85
         * @param element to check
86
         * @param operation the operation which should be tested
87
         * @return a {@link Result} holding the details.
88
         */
89
        WhiteListQuery.Result check(@NonNull ElementHandle<?> element, @NonNull WhiteListQuery.Operation operation);
90
91
        /**
92
         * Adds {@link ChangeListener} to white list.
93
         * @param listener to be added
94
         */
95
        void addChangeListener(@NonNull final ChangeListener listener);
96
97
        /**
98
         * Removes {@link ChangeListener} from white list.
99
         * @param listener to be removed
100
         */
101
        void removeChangeListener(@NonNull final ChangeListener listener);
102
    }
103
104
}
(-)a/whitelist/src/org/netbeans/spi/whitelist/support/WhiteListQueryMergerSupport.java (+199 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.spi.whitelist.support;
43
44
import java.lang.ref.Reference;
45
import java.lang.ref.WeakReference;
46
import java.util.IdentityHashMap;
47
import java.util.Map;
48
import java.util.WeakHashMap;
49
import javax.swing.event.ChangeEvent;
50
import javax.swing.event.ChangeListener;
51
import org.netbeans.api.annotations.common.NonNull;
52
import org.netbeans.api.java.source.ElementHandle;
53
import org.netbeans.api.whitelist.WhiteListQuery;
54
import org.netbeans.spi.whitelist.WhiteListQueryImplementation;
55
import org.netbeans.spi.whitelist.WhiteListQueryImplementation.WhiteListImplementation;
56
import org.netbeans.spi.project.LookupMerger;
57
import org.openide.filesystems.FileObject;
58
import org.openide.util.ChangeSupport;
59
import org.openide.util.Lookup;
60
import org.openide.util.LookupEvent;
61
import org.openide.util.LookupListener;
62
import org.openide.util.WeakListeners;
63
64
/**
65
 * Support for writing {@link WhiteListQueryImplementation}.
66
 * @author David Konecny
67
 * @author Tomas Zezula
68
 */
69
public class WhiteListQueryMergerSupport {
70
71
    /**
72
     * Placed in a lookup this class will merge all other WhiteListQueryImplementation
73
     * registered in the same lookup. All individual WhiteListQueryImplementation must
74
     * allow invocation and/or overriding of a method to get positive return.
75
     */
76
    public static LookupMerger<WhiteListQueryImplementation> createWhiteListQueryMerger() {
77
        return new WhiteListQueryMerger();
78
    }
79
    
80
    private static class WhiteListQueryMerger implements LookupMerger<WhiteListQueryImplementation> {
81
82
        public Class<WhiteListQueryImplementation> getMergeableClass() {
83
            return WhiteListQueryImplementation.class;
84
        }
85
86
        public WhiteListQueryImplementation merge(Lookup lookup) {
87
            return new WhiteListQueryImplementationMerged(lookup);
88
        }
89
    }
90
    
91
    private static class WhiteListQueryImplementationMerged implements WhiteListQueryImplementation {
92
        private Lookup lkp;
93
        private final Map<FileObject,Reference<WhiteListImplementation>> canonicalCache;
94
95
        public WhiteListQueryImplementationMerged(Lookup lkp) {
96
            this.lkp = lkp;
97
            this.canonicalCache = new WeakHashMap<FileObject, Reference<WhiteListImplementation>>();
98
        }
99
100
        @Override
101
        public synchronized WhiteListImplementation getWhiteList(final FileObject file) {
102
            final Reference<WhiteListImplementation> ref = canonicalCache.get(file);
103
            WhiteListImplementation wl = ref == null ? null : ref.get();
104
            if (wl != null) {
105
                return wl;
106
            }
107
            final Lookup.Result<WhiteListQueryImplementation> lr = lkp.lookupResult(WhiteListQueryImplementation.class);
108
            boolean empty = true;
109
            for (WhiteListQueryImplementation impl : lr.allInstances()) {
110
                WhiteListImplementation i = impl.getWhiteList(file);
111
                if (i != null) {
112
                    empty = false;
113
                    break;
114
                }
115
            }
116
            if (empty) {
117
                return null;
118
            }
119
            wl = new WhiteListImplementationMerged(lr,file);
120
            canonicalCache.put(file,new WeakReference<WhiteListImplementation>(wl));
121
            return wl;
122
        }
123
    }
124
    
125
    private static class WhiteListImplementationMerged implements WhiteListImplementation, ChangeListener, LookupListener {
126
127
        private final Lookup.Result<WhiteListQueryImplementation> lr;
128
        private final FileObject file;
129
        private final ChangeSupport changeSupport;
130
        //@GuardedBy("this")
131
        private Map<WhiteListImplementation,ChangeListener> cache;
132
133
        @SuppressWarnings("LeakingThisInConstructor")
134
        public WhiteListImplementationMerged(
135
            @NonNull final Lookup.Result<WhiteListQueryImplementation> lr,
136
            @NonNull final FileObject file) {
137
            this.lr = lr;
138
            this.file = file;
139
            this.changeSupport = new ChangeSupport(this);
140
            this.lr.addLookupListener(WeakListeners.create(LookupListener.class, this, this.lr));
141
        }
142
143
        @Override
144
        @NonNull
145
        public WhiteListQuery.Result check(
146
                @NonNull final ElementHandle<?> element,
147
                @NonNull final WhiteListQuery.Operation operation) {
148
            for (WhiteListImplementation impl : getWhiteLists()) {
149
                WhiteListQuery.Result r = impl.check(element, operation);
150
                if (r != null) {
151
                    return r;
152
                }
153
            }
154
            return null;
155
        }
156
157
        @Override
158
        public void addChangeListener(@NonNull final ChangeListener listener) {
159
            this.changeSupport.addChangeListener(listener);
160
        }
161
162
        @Override
163
        public void removeChangeListener(@NonNull final ChangeListener listener) {
164
            this.changeSupport.removeChangeListener(listener);
165
        }
166
167
        @Override
168
        public void stateChanged(final ChangeEvent event) {
169
            this.changeSupport.fireChange();
170
        }
171
172
        @Override
173
        public void resultChanged(LookupEvent ev) {
174
            this.changeSupport.fireChange();
175
        }
176
177
        private Iterable<WhiteListImplementation> getWhiteLists() {
178
            synchronized (this) {
179
                if (cache != null) {
180
                    return cache.keySet();
181
                }
182
            }
183
            final Map<WhiteListImplementation,ChangeListener> map = new IdentityHashMap<WhiteListImplementation,ChangeListener>();
184
            for (WhiteListQueryImplementation wlq : lr.allInstances()) {
185
                final WhiteListImplementation wl = wlq.getWhiteList(file);
186
                final ChangeListener cl = WeakListeners.change(this, wl);
187
                wl.addChangeListener(cl);
188
                map.put(wl, cl);
189
            }
190
            synchronized (this) {
191
                if (cache == null) {
192
                    cache = map;
193
                }
194
                return cache.keySet();
195
            }
196
        }
197
    }
198
    
199
}

Return to bug 201340