rmannibucau commented on a change in pull request #721: URL: https://github.com/apache/cxf/pull/721#discussion_r524922001
########## File path: core/src/main/java/org/apache/cxf/common/spi/ClassGeneratorClassLoader.java ########## @@ -0,0 +1,151 @@ +/** + * 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.cxf.common.spi; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.cxf.Bus; +import org.apache.cxf.BusFactory; + +public class ClassGeneratorClassLoader { + //TODO handle that with system property + private static final boolean DEBUG = false; + protected final Bus bus; + + public ClassGeneratorClassLoader(final Bus bus) { + this.bus = bus == null ? BusFactory.getDefaultBus() : bus; + } + + private String getFilePath(String s) { + String sep = System.getProperty("file.separator"); + String relativePath = s.replace('.', sep.charAt(0)); + String userDir = System.getProperty("user.dir"); + return userDir + sep + "target" + sep + "dump" + sep + relativePath + ".class"; + } + private void saveClass(String className, byte[] bytes) { + + File file; + try { + String classFileName = getFilePath(className); + String finalFileName = classFileName; + file = new File(finalFileName); + int i = 1; + while (file.exists()) { + finalFileName = classFileName.substring(0, classFileName.length() - 6) + String.valueOf(i) + ".class"; + file = new File(finalFileName); + i++; + } + file.getParentFile().mkdirs(); + try (FileOutputStream fop = new FileOutputStream(file)) { + file.createNewFile(); + fop.write(bytes); + fop.flush(); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + protected Class<?> loadClass(String className, byte[] bytes) { + if (DEBUG) { + saveClass(className, bytes); + } + TypeHelperClassLoader loader = getOrCreateLoader(); + synchronized (loader) { + Class<?> cls = loader.lookupDefinedClass(className); + if (cls == null) { + return loader.defineClass(className, bytes); + } + return cls; + } + } + protected Class<?> findClass(String className) { + return getOrCreateLoader().lookupDefinedClass(className); + } + private TypeHelperClassLoader getOrCreateLoader() { + TypeHelperClassLoader loader = bus.getExtension(TypeHelperClassLoader.class); + if (loader == null) { + synchronized (bus) { Review comment: we must not have 2 loaders so the pattern must be a computeIfAbsent. I don't think it is exposed by the bus so having a synchro point these is good - but can be on "this", just needs a constant ref in this bus context. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
