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 132508
Collapse All | Expand All

(-)eac5bb67c125 (+155 lines)
Added Link Here
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
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 * 
35
 * Contributor(s):
36
 * 
37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
38
 */
39
40
package org.openide.filesystems;
41
42
import java.io.FileDescriptor;
43
import java.io.PrintWriter;
44
import java.io.StringWriter;
45
import java.security.Permission;
46
import junit.framework.Assert;
47
48
/**
49
 *
50
 * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
51
 */
52
final class CountingSecurityManager extends SecurityManager {
53
    private static int cnt;
54
    private static StringWriter msgs;
55
    private static PrintWriter pw;
56
    private static String prefix;
57
    
58
    public static void register() {
59
        initialize("NONE");
60
    }
61
    
62
    public static void initialize(String prefix) {
63
        Assert.assertNotNull(prefix);
64
        
65
        if (! (System.getSecurityManager() instanceof CountingSecurityManager)) {
66
            setAllowedReplace(true);
67
            System.setSecurityManager(new CountingSecurityManager());
68
            setAllowedReplace(false);
69
        }
70
        if (!System.getSecurityManager().getClass().getName().equals(CountingSecurityManager.class.getName())) {
71
            throw new IllegalStateException("Wrong security manager: " + System.getSecurityManager());
72
        }
73
        cnt = 0;
74
        msgs = new StringWriter();
75
        pw = new PrintWriter(msgs);
76
        CountingSecurityManager.prefix = prefix;
77
        Statistics.reset();
78
    }
79
    
80
    public static int assertCounts(String msg, int expectedCnt) {
81
        int real = cnt;
82
        msgs = new StringWriter();
83
        pw = new PrintWriter(msgs);
84
        Statistics.getDefault().print(pw);
85
        if (cnt < expectedCnt / 10) {
86
            throw new AssertionError("Too small expectations:\n" + msg + "\n" + msgs + " exp: " + expectedCnt + " was: " + cnt);
87
        }
88
        if (expectedCnt < cnt) {
89
            throw new AssertionError(msg + "\n" + msgs + " exp: " + expectedCnt + " was: " + cnt);
90
        }
91
        cnt = 0;
92
        msgs = new StringWriter();
93
        pw = new PrintWriter(msgs);
94
        Statistics.getDefault().print(pw);
95
        return real;
96
    }
97
98
    @Override
99
    public void checkRead(String file) {
100
        if (file.startsWith(prefix)) {
101
            cnt++;
102
            Statistics.fileIsDirectory(file);
103
//            pw.println("checkRead: " + file);
104
//            new Exception().printStackTrace(pw);
105
        }
106
    }
107
108
    @Override
109
    public void checkRead(String file, Object context) {
110
        if (file.startsWith(prefix)) {
111
            cnt++;
112
            Statistics.fileIsDirectory(file);
113
            pw.println("checkRead2: " + file);
114
        }
115
    }
116
117
    @Override
118
    public void checkWrite(FileDescriptor fd) {
119
        cnt++;
120
        pw.println("Fd: " + fd);
121
    }
122
123
    @Override
124
    public void checkWrite(String file) {
125
        if (file.startsWith(prefix)) {
126
            cnt++;
127
            Statistics.fileIsDirectory(file);
128
            pw.println("checkWrite: " + file);
129
        }
130
    }
131
132
    @Override
133
    public void checkPermission(Permission perm) {
134
        if (perm.getName().equals("setSecurityManager")) { // NOI18N - hardcoded in java.lang
135
            if (!isAllowedReplace()) {
136
                throw new SecurityException();
137
            }
138
        }
139
        if (perm.getName().equals("exitVM")) {
140
            throw new SecurityException();
141
        }
142
    }
143
144
    @Override
145
    public void checkPermission(Permission perm, Object context) {
146
    }
147
148
    private static boolean isAllowedReplace() {
149
        return Boolean.getBoolean("CountingSecurityManager.allowReplace");
150
    }
151
152
    private static void setAllowedReplace(boolean aAllowedReplace) {
153
        System.setProperty("CountingSecurityManager.allowReplace", String.valueOf(aAllowedReplace));
154
    }
155
}
(-)a/openide.filesystems/test/unit/src/org/openide/filesystems/FileObjectTestHid.java (-4 / +13 lines)
Lines 1014-1023 public class FileObjectTestHid extends T Link Here
1014
        checkSetUp();
1014
        checkSetUp();
1015
        FileObject fo = getTestFile1(root);
1015
        FileObject fo = getTestFile1(root);
1016
        MockServices.setServices(MR.class);
1016
        MockServices.setServices(MR.class);
1017
        
1017
    
1018
        String actualMT = fo.getMIMEType();
1018
        File file = FileUtil.toFile(root);
1019
        assertNotNull("queried", MR.tested);
1019
        if (file != null) {
1020
        assertEquals("right mime type", "ahoj", actualMT);
1020
            CountingSecurityManager.initialize(file.getPath());
1021
        }
1022
        for (int i = 0; i < 100; i++) {
1023
            String actualMT = fo.getMIMEType();
1024
            assertNotNull("queried", MR.tested);
1025
            assertEquals("right mime type", "ahoj", actualMT);
1026
        }
1027
        if (file != null) {
1028
            CountingSecurityManager.assertCounts("The query for mime type shall be cached", 5);
1029
        }
1021
    }
1030
    }
1022
1031
1023
    public void DISABLEDtestGetMIMETypeWithResolverWhileOpenOutputStream() throws Exception {
1032
    public void DISABLEDtestGetMIMETypeWithResolverWhileOpenOutputStream() throws Exception {
(-)eac5bb67c125 (+213 lines)
Added Link Here
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.openide.filesystems;
42
43
import java.io.*;
44
import java.util.*;
45
import java.util.logging.Level;
46
import java.util.logging.Logger;
47
48
/**
49
 * Collects data and print them when JVM shutting down.
50
 * 
51
 * @author Pavel Flaška
52
 */
53
final class Statistics {
54
55
    private static final boolean streamLog = false;
56
    private static final boolean dirLog = true;
57
    private static final boolean streamCreation = false;
58
    /** singleton instance */
59
    private static Statistics INSTANCE;
60
    private static int absoluteStacks;
61
    private Map<String, Integer> isDirInvoc = new HashMap<String, Integer>();
62
    private Map<Closeable, Integer> streams = new HashMap<Closeable, Integer>();
63
    private Map<String, Integer> stacks = new HashMap<String, Integer>();
64
65
    private Statistics() {
66
    }
67
68
    /**
69
     * Get the class instance.
70
     * 
71
     * @return singleton of Statistics class.
72
     */
73
    static synchronized Statistics getDefault() {
74
        if (INSTANCE == null) {
75
            INSTANCE = new Statistics();
76
        }
77
        return INSTANCE;
78
    }
79
    
80
    static synchronized void reset() {
81
        INSTANCE = null;
82
    }
83
84
    /**
85
     * Counts in isDirectory() call on <tt>file</tt>.
86
     * 
87
     * @param file  file name
88
     */
89
    public static void fileIsDirectory(String file) {
90
        if (!dirLog) {
91
            return;
92
        }
93
        Integer i = Statistics.getDefault().isDirInvoc.get(file);
94
        if (i == null) {
95
            i = 1;
96
        } else {
97
            i++;
98
        }
99
        Statistics.getDefault().isDirInvoc.put(file, i);
100
101
        ////////////////////
102
        StringBuilder sb = new StringBuilder(300);
103
        StackTraceElement[] ste = Thread.currentThread().getStackTrace();
104
        for (i = 2; i < ste.length; i++) {
105
            sb.append(ste[i].toString()).append('\n');
106
        }
107
        String s = sb.toString();
108
        i = Statistics.getDefault().stacks.get(s);
109
        if (i == null) {
110
            i = 1;
111
        } else {
112
            i++;
113
        }
114
        Statistics.getDefault().stacks.put(s, i);
115
    }
116
117
    /**
118
     * Counts in isDirectory() call on <tt>file</tt>.
119
     * 
120
     * @param file  file name
121
     */
122
//    public static void fileIsDirectory(String file) {
123
//        StringBuilder sb = new StringBuilder(300);
124
//        StackTraceElement[] ste = Thread.currentThread().getStackTrace();
125
////        if ("InstalledFileLocatorImpl.java".equals(ste[6].getFileName())) {
126
//        if (file.endsWith(".jar")) {
127
//            Integer i = Statistics.getDefault().isDirInvoc.get(file);
128
//            if (i == null) {
129
//                i = 1;
130
//            } else {
131
//                i++;
132
//            }
133
//            Statistics.getDefault().isDirInvoc.put(file, i);
134
//            for (i = 2; i < ste.length; i++) {
135
//                sb.append(ste[i].toString()).append('\n');
136
//            }
137
//            String s = sb.toString();
138
//            i = Statistics.getDefault().stacks.get(s);
139
//            if (i == null) {
140
//                i = 1;
141
//            } else {
142
//                i++;
143
//            }
144
//            Statistics.getDefault().stacks.put(s, i);
145
//        }
146
//    }
147
    /**
148
     * Counts in read() call on <tt>inputStream</tt>.
149
     * 
150
     * @param inputStream  file name
151
     */
152
    public static void streams(Closeable closeable) {
153
        if (!streamLog) {
154
            return;
155
        }
156
        Integer i = Statistics.getDefault().streams.get(closeable);
157
        if (i == null) {
158
            i = 1;
159
        } else {
160
            i++;
161
        }
162
        Statistics.getDefault().streams.put(closeable, i);
163
164
        ////////////////////
165
        StringBuilder sb = new StringBuilder();
166
        StackTraceElement[] ste = Thread.currentThread().getStackTrace();
167
        for (i = 2; i < ste.length; i++) {
168
            sb.append(ste[i].toString()).append('\n');
169
        }
170
        String s = sb.toString();
171
        i = Statistics.getDefault().stacks.get(s);
172
        if (i == null) {
173
            i = 1;
174
        } else {
175
            i++;
176
        }
177
        Statistics.getDefault().stacks.put(s, i);
178
    }
179
    private Map<Closeable, String> streamToName = new IdentityHashMap<Closeable, String>();
180
    private Map<Closeable, StackTraceElement[]> streamDump = new IdentityHashMap<Closeable, StackTraceElement[]>();
181
182
    public static void streamToName(Closeable is, String name) {
183
        Statistics.getDefault().streamToName.put(is, name);
184
        if (streamCreation) Statistics.getDefault().streamDump.put(is, Thread.currentThread().getStackTrace());
185
    }
186
187
    ////////////////////////////////////////////////////////////////////////////
188
    // private members
189
    void print(PrintWriter out) {
190
        for (String s : isDirInvoc.keySet()) {
191
            out.printf("%4d", isDirInvoc.get(s));
192
            out.println("; " + s);
193
        }
194
        for (String s : stacks.keySet()) {
195
            int value = stacks.get(s);
196
            absoluteStacks += value;
197
            out.printf("count %5d; Stack:\n", value);
198
            out.println(s);
199
        }
200
        out.println("Total stacks recorded: " + absoluteStacks);
201
        for (Closeable s : streams.keySet()) {
202
            out.printf("count %5d", streams.get(s));
203
            out.println("; " + s.getClass() + ' ' + streamToName.get(s));
204
        }
205
        for (Closeable s : streamDump.keySet()) {
206
            out.println(streamToName.get(s));
207
            for (StackTraceElement ste : streamDump.get(s)) {
208
                out.println(ste);
209
            }
210
            out.println();
211
        }
212
    }
213
}

Return to bug 132508