Author: mbenson
Date: Sat Apr 27 23:20:25 2013
New Revision: 1476695
URL: http://svn.apache.org/r1476695
Log:
add PrivilizerCleaner
Added:
commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java
commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Cleaner
Modified:
commons/sandbox/weaver/trunk/modules/privilizer/weaver/pom.xml
commons/sandbox/weaver/trunk/pom.xml
Modified: commons/sandbox/weaver/trunk/modules/privilizer/weaver/pom.xml
URL:
http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/modules/privilizer/weaver/pom.xml?rev=1476695&r1=1476694&r2=1476695&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/modules/privilizer/weaver/pom.xml (original)
+++ commons/sandbox/weaver/trunk/modules/privilizer/weaver/pom.xml Sat Apr 27
23:20:25 2013
@@ -48,6 +48,10 @@
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>asm</artifactId>
+ </dependency>
</dependencies>
<build>
Added:
commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java?rev=1476695&view=auto
==============================================================================
---
commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java
(added)
+++
commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java
Sat Apr 27 23:20:25 2013
@@ -0,0 +1,120 @@
+/*
+ * Copyright the original author or authors.
+ *
+ * Licensed 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.commons.weaver.privilizer;
+
+import java.io.File;
+import java.lang.annotation.ElementType;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.weaver.model.ScanRequest;
+import org.apache.commons.weaver.model.ScanResult;
+import org.apache.commons.weaver.model.WeavableClass;
+import org.apache.commons.weaver.model.WeaveInterest;
+import org.apache.commons.weaver.privilizer.Privilizer.Policy;
+import org.apache.commons.weaver.privilizer.Privilizer.Privilized;
+import org.apache.commons.weaver.spi.Cleaner;
+import org.apache.commons.weaver.utils.URLArray;
+import org.apache.xbean.finder.archive.FileArchive;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.Opcodes;
+
+/**
+ * Removes classes privilized with a different policy.
+ */
+/*
+ * Implemented with ASM in anticipation of the rest of the privilizer being
rewritten :P
+ */
+public class PrivilizerCleaner implements Cleaner {
+ private static final int ASM_FLAGS = ClassReader.SKIP_CODE +
ClassReader.SKIP_DEBUG + ClassReader.SKIP_FRAMES;
+ private static final Logger LOG =
Logger.getLogger(PrivilizerCleaner.class.getName());
+
+ private File target;
+ private Privilizer.Policy policy;
+ private FileArchive fileArchive;
+
+ @Override
+ public void configure(List<String> classpath, File target, Properties
config) {
+ final ClassLoader classLoader = new
URLClassLoader(URLArray.fromPaths(classpath));
+ fileArchive = new FileArchive(classLoader, target);
+
+ final String policyConfig =
config.getProperty(PrivilizerWeaver.CONFIG_POLICY);
+ policy =
+ StringUtils.isEmpty(policyConfig) ?
Privilizer.Policy.defaultValue() : Privilizer.Policy
+ .valueOf(policyConfig);
+ this.target = target;
+ }
+
+ @Override
+ public ScanRequest getScanRequest() {
+ return new ScanRequest().add(WeaveInterest.of(Privilized.class,
ElementType.TYPE));
+ }
+
+ @Override
+ public boolean clean(ScanResult scanResult) {
+ final List<String> toDelete = new ArrayList<String>();
+
+ LOG.log(Level.FINE, "Cleaning classes privilized with policy other
than {0}", policy);
+ for (WeavableClass<?> weavableClass :
scanResult.getClasses().with(Privilized.class)) {
+ final Policy privilizedPolicy =
weavableClass.getAnnotation(Privilized.class).value();
+ if (privilizedPolicy == policy) {
+ continue;
+ }
+ final String className = weavableClass.getTarget().getName();
+ LOG.log(Level.FINE, "Class {0} privilized with {1}; deleting.",
+ new Object[] { className, privilizedPolicy });
+
+ try {
+ final ClassReader classReader = new
ClassReader(fileArchive.getBytecode(className));
+ classReader.accept(new ClassVisitor(Opcodes.ASM4) {
+ @Override
+ public void visit(int version, int access, String name,
String signature, String superName,
+ String[] interfaces) {
+ toDelete.add(name);
+ }
+
+ @Override
+ public void visitInnerClass(String name, String outerName,
String innerName, int access) {
+ if (toDelete.contains(outerName)) {
+ toDelete.add(name);
+ }
+ }
+ }, ASM_FLAGS);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ boolean result = false;
+ for (String className : toDelete) {
+ final File classfile = new File(target, toResourcePath(className));
+ final boolean success = classfile.delete();
+ LOG.log(Level.FINE, "Deletion of {0} was {1}.", new Object[] {
classfile,
+ success ? "successful" : "unsuccessful" });
+ result |= success;
+ }
+ return result;
+ }
+
+ private static String toResourcePath(String className) {
+ return className.replace('.', '/') + ".class";
+ }
+}
Added:
commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Cleaner
URL:
http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Cleaner?rev=1476695&view=auto
==============================================================================
---
commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Cleaner
(added)
+++
commons/sandbox/weaver/trunk/modules/privilizer/weaver/src/main/resources/META-INF/services/org.apache.commons.weaver.spi.Cleaner
Sat Apr 27 23:20:25 2013
@@ -0,0 +1,19 @@
+# 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.
+
+# this class gets picked up by the WeaveProcessor
+org.apache.commons.weaver.privilizer.PrivilizerCleaner
Modified: commons/sandbox/weaver/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/pom.xml?rev=1476695&r1=1476694&r2=1476695&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/pom.xml (original)
+++ commons/sandbox/weaver/trunk/pom.xml Sat Apr 27 23:20:25 2013
@@ -99,6 +99,11 @@
<version>3.17.1-GA</version>
</dependency>
<dependency>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>asm</artifactId>
+ <version>4.1</version>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>