Clean up.
authorTomas Zezula <tzezula@netbeans.org>
Wed, 29 Jan 2014 16:49:33 +0100
changeset 18129a98f3d32a1bb
parent 18128 aea99e83722b
child 18130 93c0074459db
child 18131 8e32d397f1e1
Clean up.
dew4nb/src/org/netbeans/modules/dew4nb/JavacServer.java
dew4nb/src/org/netbeans/modules/dew4nb/LaunchDew4Nb.java
dew4nb/src/org/netbeans/modules/dew4nb/OpenProjectsWorkspaceResolver.java
     1.1 --- a/dew4nb/src/org/netbeans/modules/dew4nb/JavacServer.java	Wed Jan 29 16:44:24 2014 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,92 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
     1.8 - *
     1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 - * Other names may be trademarks of their respective owners.
    1.11 - *
    1.12 - * The contents of this file are subject to the terms of either the GNU
    1.13 - * General Public License Version 2 only ("GPL") or the Common
    1.14 - * Development and Distribution License("CDDL") (collectively, the
    1.15 - * "License"). You may not use this file except in compliance with the
    1.16 - * License. You can obtain a copy of the License at
    1.17 - * http://www.netbeans.org/cddl-gplv2.html
    1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 - * specific language governing permissions and limitations under the
    1.20 - * License.  When distributing the software, include this License Header
    1.21 - * Notice in each file and include the License file at
    1.22 - * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 - * particular file as subject to the "Classpath" exception as provided
    1.24 - * by Oracle in the GPL Version 2 section of the License file that
    1.25 - * accompanied this code. If applicable, add the following below the
    1.26 - * License Header, with the fields enclosed by brackets [] replaced by
    1.27 - * your own identifying information:
    1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 - *
    1.30 - * If you wish your version of this file to be governed by only the CDDL
    1.31 - * or only the GPL Version 2, indicate your decision by adding
    1.32 - * "[Contributor] elects to include this software in this distribution
    1.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.34 - * single choice of license, a recipient has the option to distribute
    1.35 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.36 - * to extend the choice of license to its licensees as provided above.
    1.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.38 - * Version 2 license, then the option applies only if the new code is
    1.39 - * made subject to such option by the copyright holder.
    1.40 - *
    1.41 - * Contributor(s):
    1.42 - *
    1.43 - * Portions Copyrighted 2013 Sun Microsystems, Inc.
    1.44 - */
    1.45 -
    1.46 -package org.netbeans.modules.dew4nb;
    1.47 -
    1.48 -import java.io.File;
    1.49 -import java.io.IOException;
    1.50 -import org.netbeans.modules.dew4nb.api.Server;
    1.51 -import org.openide.modules.InstalledFileLocator;
    1.52 -import org.openide.util.Exceptions;
    1.53 -
    1.54 -/** HTTP and WebSocket server for answering Javac like queries.
    1.55 - *
    1.56 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.57 - */
    1.58 -final class JavacServer {
    1.59 -    private static JavacServer INSTANCE;
    1.60 -    private final Server server;
    1.61 -    private JavacServer(Server s) {
    1.62 -        this.server = s;
    1.63 -        try {
    1.64 -            this.server.start();
    1.65 -        } catch (IOException ex) {
    1.66 -            Exceptions.printStackTrace(ex);
    1.67 -        }
    1.68 -    }
    1.69 -    
    1.70 -    public synchronized static JavacServer getDefault() {
    1.71 -        if (INSTANCE == null) {
    1.72 -            final String web4dew = System.getProperty("web4dew"); //NOI18N
    1.73 -            final File root;
    1.74 -            final boolean cache;
    1.75 -            if (web4dew == null) {
    1.76 -                final File index = InstalledFileLocator.getDefault().locate("web4dew/index.html", "org.netbeans.modules.dew4nb", true);
    1.77 -                root = index.getParentFile();
    1.78 -                cache = true;
    1.79 -            } else {
    1.80 -                root = new File(web4dew);
    1.81 -                cache = false;
    1.82 -            }
    1.83 -            INSTANCE = new JavacServer(
    1.84 -                Server.createBuilder().
    1.85 -                setPortRange(9000, 65535).
    1.86 -                addStaticHttpRoot(root, cache).
    1.87 -                build());
    1.88 -        }
    1.89 -        return INSTANCE;
    1.90 -    }
    1.91 -    
    1.92 -    public int getPort() {
    1.93 -        return server.getPort();
    1.94 -    }           
    1.95 -}
     2.1 --- a/dew4nb/src/org/netbeans/modules/dew4nb/LaunchDew4Nb.java	Wed Jan 29 16:44:24 2014 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,76 +0,0 @@
     2.4 -/*
     2.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 - *
     2.7 - * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
     2.8 - *
     2.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 - * Other names may be trademarks of their respective owners.
    2.11 - *
    2.12 - * The contents of this file are subject to the terms of either the GNU
    2.13 - * General Public License Version 2 only ("GPL") or the Common
    2.14 - * Development and Distribution License("CDDL") (collectively, the
    2.15 - * "License"). You may not use this file except in compliance with the
    2.16 - * License. You can obtain a copy of the License at
    2.17 - * http://www.netbeans.org/cddl-gplv2.html
    2.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 - * specific language governing permissions and limitations under the
    2.20 - * License.  When distributing the software, include this License Header
    2.21 - * Notice in each file and include the License file at
    2.22 - * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 - * particular file as subject to the "Classpath" exception as provided
    2.24 - * by Oracle in the GPL Version 2 section of the License file that
    2.25 - * accompanied this code. If applicable, add the following below the
    2.26 - * License Header, with the fields enclosed by brackets [] replaced by
    2.27 - * your own identifying information:
    2.28 - * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 - *
    2.30 - * If you wish your version of this file to be governed by only the CDDL
    2.31 - * or only the GPL Version 2, indicate your decision by adding
    2.32 - * "[Contributor] elects to include this software in this distribution
    2.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.34 - * single choice of license, a recipient has the option to distribute
    2.35 - * your version of this file under either the CDDL, the GPL Version 2 or
    2.36 - * to extend the choice of license to its licensees as provided above.
    2.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.38 - * Version 2 license, then the option applies only if the new code is
    2.39 - * made subject to such option by the copyright holder.
    2.40 - *
    2.41 - * Contributor(s):
    2.42 - *
    2.43 - * Portions Copyrighted 2013 Sun Microsystems, Inc.
    2.44 - */
    2.45 -package org.netbeans.modules.dew4nb;
    2.46 -
    2.47 -import java.awt.event.ActionEvent;
    2.48 -import java.awt.event.ActionListener;
    2.49 -import java.net.MalformedURLException;
    2.50 -import java.net.URL;
    2.51 -import org.openide.awt.ActionID;
    2.52 -import org.openide.awt.ActionReference;
    2.53 -import org.openide.awt.ActionRegistration;
    2.54 -import org.openide.awt.HtmlBrowser.URLDisplayer;
    2.55 -import org.openide.util.Exceptions;
    2.56 -import org.openide.util.NbBundle.Messages;
    2.57 -
    2.58 -@ActionID(
    2.59 -        category = "Tools",
    2.60 -        id = "org.netbeans.modules.dew4nb.LaunchDew4Nb"
    2.61 -)
    2.62 -@ActionRegistration(
    2.63 -        iconBase = "org/netbeans/modules/dew4nb/dew.png",
    2.64 -        displayName = "#CTL_LaunchDew4Nb"
    2.65 -)
    2.66 -@ActionReference(path = "Menu/Tools", position = 1392, separatorBefore = 1350)
    2.67 -@Messages("CTL_LaunchDew4Nb=Development Environment for Web")
    2.68 -public final class LaunchDew4Nb implements ActionListener {
    2.69 -
    2.70 -    @Override
    2.71 -    public void actionPerformed(ActionEvent e) {
    2.72 -        try {
    2.73 -            URL u = new URL("http://localhost:" + JavacServer.getDefault().getPort());
    2.74 -            URLDisplayer.getDefault().showURLExternal(u);
    2.75 -        } catch (MalformedURLException ex) {
    2.76 -            Exceptions.printStackTrace(ex);
    2.77 -        }
    2.78 -    }
    2.79 -}
     3.1 --- a/dew4nb/src/org/netbeans/modules/dew4nb/OpenProjectsWorkspaceResolver.java	Wed Jan 29 16:44:24 2014 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,178 +0,0 @@
     3.4 -/*
     3.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 - *
     3.7 - * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
     3.8 - *
     3.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    3.10 - * Other names may be trademarks of their respective owners.
    3.11 - *
    3.12 - * The contents of this file are subject to the terms of either the GNU
    3.13 - * General Public License Version 2 only ("GPL") or the Common
    3.14 - * Development and Distribution License("CDDL") (collectively, the
    3.15 - * "License"). You may not use this file except in compliance with the
    3.16 - * License. You can obtain a copy of the License at
    3.17 - * http://www.netbeans.org/cddl-gplv2.html
    3.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.19 - * specific language governing permissions and limitations under the
    3.20 - * License.  When distributing the software, include this License Header
    3.21 - * Notice in each file and include the License file at
    3.22 - * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    3.23 - * particular file as subject to the "Classpath" exception as provided
    3.24 - * by Oracle in the GPL Version 2 section of the License file that
    3.25 - * accompanied this code. If applicable, add the following below the
    3.26 - * License Header, with the fields enclosed by brackets [] replaced by
    3.27 - * your own identifying information:
    3.28 - * "Portions Copyrighted [year] [name of copyright owner]"
    3.29 - *
    3.30 - * If you wish your version of this file to be governed by only the CDDL
    3.31 - * or only the GPL Version 2, indicate your decision by adding
    3.32 - * "[Contributor] elects to include this software in this distribution
    3.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.34 - * single choice of license, a recipient has the option to distribute
    3.35 - * your version of this file under either the CDDL, the GPL Version 2 or
    3.36 - * to extend the choice of license to its licensees as provided above.
    3.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.38 - * Version 2 license, then the option applies only if the new code is
    3.39 - * made subject to such option by the copyright holder.
    3.40 - *
    3.41 - * Contributor(s):
    3.42 - *
    3.43 - * Portions Copyrighted 2013 Sun Microsystems, Inc.
    3.44 - */
    3.45 -
    3.46 -package org.netbeans.modules.dew4nb;
    3.47 -
    3.48 -import java.io.IOException;
    3.49 -import java.lang.ref.Reference;
    3.50 -import java.lang.ref.WeakReference;
    3.51 -import java.net.URL;
    3.52 -import java.util.Collections;
    3.53 -import java.util.Set;
    3.54 -import java.util.WeakHashMap;
    3.55 -import java.util.logging.Logger;
    3.56 -import org.netbeans.api.annotations.common.CheckForNull;
    3.57 -import org.netbeans.api.annotations.common.NonNull;
    3.58 -import org.netbeans.api.java.classpath.ClassPath;
    3.59 -import org.netbeans.modules.dew4nb.spi.WorkspaceResolver;
    3.60 -import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
    3.61 -import org.openide.filesystems.FileObject;
    3.62 -import org.openide.filesystems.FileUtil;
    3.63 -import org.openide.filesystems.URLMapper;
    3.64 -import org.openide.util.Pair;
    3.65 -import org.openide.util.Parameters;
    3.66 -import org.openide.util.lookup.ServiceProvider;
    3.67 -
    3.68 -/**
    3.69 - *
    3.70 - * @author Tomas Zezula
    3.71 - */
    3.72 -@ServiceProvider(service = WorkspaceResolver.class, position = 10_000)
    3.73 -public class OpenProjectsWorkspaceResolver extends WorkspaceResolver {
    3.74 -
    3.75 -    private static final Logger LOG = Logger.getLogger(OpenProjectsWorkspaceResolver.class.getName());
    3.76 -
    3.77 -    private final Object lock = new Object();
    3.78 -
    3.79 -    //@GuardedBy("lock")
    3.80 -    private Pair<Reference<FileObject>,Reference<FileObject>> last;
    3.81 -    //@GuardedBy("lock")
    3.82 -    private final Set<FileObject> knownRoots;
    3.83 -
    3.84 -    public OpenProjectsWorkspaceResolver() {
    3.85 -        this.knownRoots = Collections.newSetFromMap(new WeakHashMap<FileObject,Boolean>());
    3.86 -    }
    3.87 -
    3.88 -    @Override
    3.89 -    @CheckForNull
    3.90 -    public FileObject resolveFile(
    3.91 -            @NonNull final Context ctx) {
    3.92 -        Parameters.notNull("ctx", ctx);   //NOI18N
    3.93 -        final FileObject srcRoot = getSourceRoot(ctx.getWorkspace());
    3.94 -        if (srcRoot == null) {
    3.95 -            return null;
    3.96 -        }
    3.97 -        return srcRoot.getFileObject(ctx.getPath());
    3.98 -    }
    3.99 -
   3.100 -    @Override
   3.101 -    @CheckForNull
   3.102 -    public Context resolveContext(@NonNull final FileObject file) {
   3.103 -        Parameters.notNull("file", file);   //NOI18N
   3.104 -        final FileObject srcRoot = getSourceRoot(file);
   3.105 -        if (srcRoot == null) {
   3.106 -            return null;
   3.107 -        }
   3.108 -        final String workspaceId = getWorkspaceId(srcRoot);
   3.109 -        if (workspaceId == null) {
   3.110 -            return null;
   3.111 -        }
   3.112 -        final String path = FileUtil.getRelativePath(srcRoot, file);
   3.113 -        return new Context(
   3.114 -            "", //NOI18N
   3.115 -            workspaceId,
   3.116 -            path);
   3.117 -    }
   3.118 -
   3.119 -
   3.120 -    @CheckForNull
   3.121 -    private FileObject getSourceRoot(@NonNull final FileObject file) {
   3.122 -        synchronized (lock) {
   3.123 -            if (last != null) {
   3.124 -                FileObject res;
   3.125 -                if (file.equals(last.first().get()) && (res=last.second().get()) != null) {
   3.126 -                    return res;
   3.127 -                }
   3.128 -            }
   3.129 -            for (FileObject root : knownRoots) {
   3.130 -                if (root.equals(file) || FileUtil.isParentOf(root, file)) {
   3.131 -                    return root;
   3.132 -                }
   3.133 -            }
   3.134 -        }
   3.135 -        final ClassPath cp = ClassPath.getClassPath(file, ClassPath.SOURCE);
   3.136 -        if (cp == null) {
   3.137 -            return null;
   3.138 -        }
   3.139 -        final FileObject root = cp.findOwnerRoot(file);
   3.140 -        if (root == null) {
   3.141 -            return null;
   3.142 -        }
   3.143 -        synchronized (lock) {
   3.144 -            last = Pair.<Reference<FileObject>,Reference<FileObject>>of(
   3.145 -                new WeakReference<FileObject>(file),
   3.146 -                new WeakReference<FileObject>(root));
   3.147 -            knownRoots.add(root);
   3.148 -        }
   3.149 -        return root;
   3.150 -    }
   3.151 -
   3.152 -    @CheckForNull
   3.153 -    private FileObject getSourceRoot(@NonNull final String workspaceId) {
   3.154 -        final FileObject cacheFolder = CacheFolder.getCacheFolder();
   3.155 -        if (cacheFolder == null) {
   3.156 -            return null;
   3.157 -        }
   3.158 -        final FileObject dataFolder = cacheFolder.getFileObject(workspaceId);
   3.159 -        if (dataFolder == null) {
   3.160 -            return null;
   3.161 -        }
   3.162 -        final URL srcRootURL = CacheFolder.getSourceRootForDataFolder(dataFolder);
   3.163 -        return srcRootURL == null ?
   3.164 -            null:
   3.165 -            URLMapper.findFileObject(srcRootURL);
   3.166 -    }
   3.167 -
   3.168 -    @CheckForNull
   3.169 -    private String getWorkspaceId(@NonNull final FileObject srcRoot) {
   3.170 -        try {
   3.171 -            FileObject dataFolder = CacheFolder.getDataFolder(srcRoot.toURL(), true);
   3.172 -            return dataFolder == null ?
   3.173 -                null :
   3.174 -                dataFolder.getName();
   3.175 -        } catch (IOException ioe) {
   3.176 -            LOG.warning(ioe.getMessage());
   3.177 -            return null;
   3.178 -        }
   3.179 -    }
   3.180 -
   3.181 -}