freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [4/5] incubator-freemarker git commit: Moved debug feature into a single package.
Date Wed, 01 Mar 2017 18:39:08 GMT
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6939fd07/src/main/java/org/apache/freemarker/core/debug/impl/RmiDebuggerListenerImpl.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/freemarker/core/debug/impl/RmiDebuggerListenerImpl.java
b/src/main/java/org/apache/freemarker/core/debug/impl/RmiDebuggerListenerImpl.java
deleted file mode 100644
index 4d7b8a7..0000000
--- a/src/main/java/org/apache/freemarker/core/debug/impl/RmiDebuggerListenerImpl.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.freemarker.core.debug.impl;
-
-import java.rmi.NoSuchObjectException;
-import java.rmi.RemoteException;
-import java.rmi.server.UnicastRemoteObject;
-import java.rmi.server.Unreferenced;
-
-import org.apache.freemarker.core._CoreLogs;
-import org.apache.freemarker.core.debug.DebuggerClient;
-import org.apache.freemarker.core.debug.DebuggerListener;
-import org.apache.freemarker.core.debug.EnvironmentSuspendedEvent;
-import org.slf4j.Logger;
-
-/**
- * Used by the {@link DebuggerClient} to create local 
- */
-public class RmiDebuggerListenerImpl
-extends
-    UnicastRemoteObject
-implements
-    DebuggerListener, Unreferenced {
-    
-    private static final Logger LOG = _CoreLogs.DEBUG_CLIENT;
-    
-    private static final long serialVersionUID = 1L;
-
-    private final DebuggerListener listener;
-
-    @Override
-    public void unreferenced() {
-        try {
-            UnicastRemoteObject.unexportObject(this, false);
-        } catch (NoSuchObjectException e) {
-            LOG.warn("Failed to unexport RMI debugger listener", e);
-        }
-    }
-    
-    public RmiDebuggerListenerImpl(DebuggerListener listener) 
-    throws RemoteException {
-        this.listener = listener;
-    }
-
-    @Override
-    public void environmentSuspended(EnvironmentSuspendedEvent e) 
-    throws RemoteException {
-        listener.environmentSuspended(e);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6939fd07/src/main/java/org/apache/freemarker/core/debug/impl/RmiDebuggerService.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/freemarker/core/debug/impl/RmiDebuggerService.java b/src/main/java/org/apache/freemarker/core/debug/impl/RmiDebuggerService.java
deleted file mode 100644
index 555fc4e..0000000
--- a/src/main/java/org/apache/freemarker/core/debug/impl/RmiDebuggerService.java
+++ /dev/null
@@ -1,310 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.freemarker.core.debug.impl;
-
-import java.io.Serializable;
-import java.lang.ref.ReferenceQueue;
-import java.lang.ref.WeakReference;
-import java.rmi.RemoteException;
-import java.rmi.server.RemoteObject;
-import java.rmi.server.UnicastRemoteObject;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.freemarker.core.Environment;
-import org.apache.freemarker.core.Template;
-import org.apache.freemarker.core._Debug;
-import org.apache.freemarker.core.debug.Breakpoint;
-import org.apache.freemarker.core.debug.DebuggerListener;
-import org.apache.freemarker.core.debug.EnvironmentSuspendedEvent;
-import org.apache.freemarker.core.util.UndeclaredThrowableException;
-
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-
-/**
- * @version $Id
- */
-class RmiDebuggerService
-extends
-    DebuggerService {
-    private final Map templateDebugInfos = new HashMap();
-    private final HashSet suspendedEnvironments = new HashSet();
-    private final Map listeners = new HashMap();
-    private final ReferenceQueue refQueue = new ReferenceQueue();
-     
-
-    private final RmiDebuggerImpl debugger;
-    private DebuggerServer server;
-
-    RmiDebuggerService() {
-        try {
-            debugger = new RmiDebuggerImpl(this);
-            server = new DebuggerServer((Serializable) RemoteObject.toStub(debugger));
-            server.start();
-        } catch (RemoteException e) {
-            e.printStackTrace();
-            throw new UndeclaredThrowableException(e);
-        }
-    }
-    
-    @Override
-    List getBreakpointsSpi(String templateName) {
-        synchronized (templateDebugInfos) {
-            TemplateDebugInfo tdi = findTemplateDebugInfo(templateName);
-            return tdi == null ? Collections.EMPTY_LIST : tdi.breakpoints;
-        }
-    }
-
-    List getBreakpointsSpi() {
-        List sumlist = new ArrayList();
-        synchronized (templateDebugInfos) {
-            for (Iterator iter = templateDebugInfos.values().iterator(); iter.hasNext();
) {
-                sumlist.addAll(((TemplateDebugInfo) iter.next()).breakpoints);
-            }
-        }
-        Collections.sort(sumlist);
-        return sumlist;
-    }
-
-    // TODO See in SuppressFBWarnings
-    @Override
-    @SuppressFBWarnings(value={ "UW_UNCOND_WAIT", "WA_NOT_IN_LOOP" }, justification="Will
have to be re-desigend; postponed.")
-    boolean suspendEnvironmentSpi(Environment env, String templateName, int line)
-    throws RemoteException {
-        RmiDebuggedEnvironmentImpl denv = 
-            (RmiDebuggedEnvironmentImpl)
-                RmiDebuggedEnvironmentImpl.getCachedWrapperFor(env);
-                
-        synchronized (suspendedEnvironments) {
-            suspendedEnvironments.add(denv);
-        }
-        try {
-            EnvironmentSuspendedEvent breakpointEvent = 
-                new EnvironmentSuspendedEvent(this, templateName, line, denv);
-    
-            synchronized (listeners) {
-                for (Iterator iter = listeners.values().iterator(); iter.hasNext(); ) {
-                    DebuggerListener listener = (DebuggerListener) iter.next();
-                    listener.environmentSuspended(breakpointEvent);
-                }
-            }
-            synchronized (denv) {
-                try {
-                    denv.wait();
-                } catch (InterruptedException e) {
-                    // Intentionally ignored
-                }
-            }
-            return denv.isStopped();
-        } finally {
-            synchronized (suspendedEnvironments) {
-                suspendedEnvironments.remove(denv);
-            }
-        }
-    }
-    
-    @Override
-    void registerTemplateSpi(Template template) {
-        String templateName = template.getName();
-        synchronized (templateDebugInfos) {
-            TemplateDebugInfo tdi = createTemplateDebugInfo(templateName);
-            tdi.templates.add(new TemplateReference(templateName, template, refQueue));
-            // Inject already defined breakpoints into the template
-            for (Iterator iter = tdi.breakpoints.iterator(); iter.hasNext(); ) {
-                Breakpoint breakpoint = (Breakpoint) iter.next();
-                _Debug.insertDebugBreak(template, breakpoint.getLine());
-            }
-        }
-    }
-    
-    Collection getSuspendedEnvironments() {
-        return (Collection) suspendedEnvironments.clone();
-    }
-
-    Object addDebuggerListener(DebuggerListener listener) {
-        Object id; 
-        synchronized (listeners) {
-            id = Long.valueOf(System.currentTimeMillis());
-            listeners.put(id, listener);
-        }
-        return id;
-    }
-    
-    void removeDebuggerListener(Object id) {
-        synchronized (listeners) {
-            listeners.remove(id);
-        }
-    }
-
-    void addBreakpoint(Breakpoint breakpoint) {
-        String templateName = breakpoint.getTemplateName();
-        synchronized (templateDebugInfos) {
-            TemplateDebugInfo tdi = createTemplateDebugInfo(templateName);
-            List breakpoints = tdi.breakpoints;
-            int pos = Collections.binarySearch(breakpoints, breakpoint);
-            if (pos < 0) {
-                // Add to the list of breakpoints
-                breakpoints.add(-pos - 1, breakpoint);
-                // Inject the breakpoint into all templates with this name
-                for (Iterator iter = tdi.templates.iterator(); iter.hasNext(); ) {
-                    TemplateReference ref = (TemplateReference) iter.next();
-                    Template t = ref.getTemplate();
-                    if (t == null) {
-                        iter.remove();
-                    } else {
-                        _Debug.insertDebugBreak(t, breakpoint.getLine());
-                    }
-                }
-            }
-        }
-    }
-
-    private TemplateDebugInfo findTemplateDebugInfo(String templateName) {
-        processRefQueue();
-        return (TemplateDebugInfo) templateDebugInfos.get(templateName); 
-    }
-    
-    private TemplateDebugInfo createTemplateDebugInfo(String templateName) {
-        TemplateDebugInfo tdi = findTemplateDebugInfo(templateName);
-        if (tdi == null) {
-            tdi = new TemplateDebugInfo();
-            templateDebugInfos.put(templateName, tdi);
-        }
-        return tdi;
-    }
-    
-    void removeBreakpoint(Breakpoint breakpoint) {
-        String templateName = breakpoint.getTemplateName();
-        synchronized (templateDebugInfos) {
-            TemplateDebugInfo tdi = findTemplateDebugInfo(templateName);
-            if (tdi != null) {
-                List breakpoints = tdi.breakpoints;
-                int pos = Collections.binarySearch(breakpoints, breakpoint);
-                if (pos >= 0) { 
-                    breakpoints.remove(pos);
-                    for (Iterator iter = tdi.templates.iterator(); iter.hasNext(); ) {
-                        TemplateReference ref = (TemplateReference) iter.next();
-                        Template t = ref.getTemplate();
-                        if (t == null) {
-                            iter.remove();
-                        } else {
-                            _Debug.removeDebugBreak(t, breakpoint.getLine());
-                        }
-                    }
-                }
-                if (tdi.isEmpty()) {
-                    templateDebugInfos.remove(templateName);
-                }
-            }
-        }
-    }
-
-    void removeBreakpoints(String templateName) {
-        synchronized (templateDebugInfos) {
-            TemplateDebugInfo tdi = findTemplateDebugInfo(templateName);
-            if (tdi != null) {
-                removeBreakpoints(tdi);
-                if (tdi.isEmpty()) {
-                    templateDebugInfos.remove(templateName);
-                }
-            }
-        }
-    }
-
-    void removeBreakpoints() {
-        synchronized (templateDebugInfos) {
-            for (Iterator iter = templateDebugInfos.values().iterator(); iter.hasNext();
) {
-                TemplateDebugInfo tdi = (TemplateDebugInfo) iter.next(); 
-                removeBreakpoints(tdi);
-                if (tdi.isEmpty()) {
-                    iter.remove();
-                }
-            }
-        }
-    }
-
-    private void removeBreakpoints(TemplateDebugInfo tdi) {
-        tdi.breakpoints.clear();
-        for (Iterator iter = tdi.templates.iterator(); iter.hasNext(); ) {
-            TemplateReference ref = (TemplateReference) iter.next();
-            Template t = ref.getTemplate();
-            if (t == null) {
-                iter.remove();
-            } else {
-                _Debug.removeDebugBreaks(t);
-            }
-        }
-    }
-
-    private static final class TemplateDebugInfo {
-        final List templates = new ArrayList();
-        final List breakpoints = new ArrayList();
-        
-        boolean isEmpty() {
-            return templates.isEmpty() && breakpoints.isEmpty();
-        }
-    }
-    
-    private static final class TemplateReference extends WeakReference {
-        final String templateName;
-         
-        TemplateReference(String templateName, Template template, ReferenceQueue queue) {
-            super(template, queue);
-            this.templateName = templateName;
-        }
-        
-        Template getTemplate() {
-            return (Template) get();
-        }
-    }
-    
-    private void processRefQueue() {
-        for (; ; ) {
-            TemplateReference ref = (TemplateReference) refQueue.poll();
-            if (ref == null) {
-                break;
-            }
-            TemplateDebugInfo tdi = findTemplateDebugInfo(ref.templateName);
-            if (tdi != null) {
-                tdi.templates.remove(ref);
-                if (tdi.isEmpty()) {
-                    templateDebugInfos.remove(ref.templateName);
-                }
-            }
-        }
-    }
-
-    @Override
-    void shutdownSpi() {
-        server.stop();
-        try {
-            UnicastRemoteObject.unexportObject(debugger, true);
-        } catch (Exception e) {
-        }
-
-        RmiDebuggedEnvironmentImpl.cleanup();
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6939fd07/src/main/java/org/apache/freemarker/core/debug/impl/SoftCache.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/freemarker/core/debug/impl/SoftCache.java b/src/main/java/org/apache/freemarker/core/debug/impl/SoftCache.java
deleted file mode 100644
index bbc1423..0000000
--- a/src/main/java/org/apache/freemarker/core/debug/impl/SoftCache.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.freemarker.core.debug.impl;
-
-import java.lang.ref.Reference;
-import java.lang.ref.ReferenceQueue;
-import java.lang.ref.SoftReference;
-import java.util.Map;
-
-class SoftCache {
-    
-    private final ReferenceQueue queue = new ReferenceQueue();
-    private final Map map;
-    
-    public SoftCache(Map backingMap) {
-        map = backingMap;
-    }
-    
-    public Object get(Object key) {
-        processQueue();
-        Reference ref = (Reference) map.get(key);
-        return ref == null ? null : ref.get();
-    }
-
-    public void put(Object key, Object value) {
-        processQueue();
-        map.put(key, new SoftValueReference(key, value, queue));
-    }
-
-    public void remove(Object key) {
-        processQueue();
-        map.remove(key);
-    }
-
-    public void clear() {
-        map.clear();
-        processQueue();
-    }
-    
-    /**
-     * Returns a close approximation of the number of cache entries.
-     */
-    public int getSize() {
-        processQueue();
-        return map.size();
-    }
-
-    private void processQueue() {
-        for (; ; ) {
-            SoftValueReference ref = (SoftValueReference) queue.poll();
-            if (ref == null) {
-                return;
-            }
-            Object key = ref.getKey();
-            map.remove(key);
-        }
-    }
-
-    private static final class SoftValueReference extends SoftReference {
-        private final Object key;
-
-        SoftValueReference(Object key, Object value, ReferenceQueue queue) {
-            super(value, queue);
-            this.key = key;
-        }
-
-        Object getKey() {
-            return key;
-        }
-    }
-    
-}
\ No newline at end of file


Mime
View raw message