use of com.opensymphony.xwork2.FileManager in project struts by apache.
the class BundlePackageLoader method loadPackages.
@Deprecated
@Override
public List<PackageConfig> loadPackages(Bundle bundle, BundleContext bundleContext, ObjectFactory objectFactory, FileManagerFactory fileManagerFactory, Map<String, PackageConfig> pkgConfigs) throws ConfigurationException {
if (pkgConfigs == null) {
// Better than a NPE.
throw new IllegalArgumentException("Cannot load packages from a null package configuration");
}
Configuration config = new DefaultConfiguration("struts.xml");
LOG.trace("LoadPackages - After config constructed. Before BundleConfigurationProvider constructed");
BundleConfigurationProvider prov = new BundleConfigurationProvider("struts.xml", bundle, bundleContext);
LOG.trace("LoadPackages - After BundleConfigurationProvider constructed. Before config.addPackageConfig loop");
pkgConfigs.values().forEach(pkg -> {
config.addPackageConfig(pkg.getName(), pkg);
});
LOG.trace("LoadPackages - After config.addPackageConfig loop. Before prov.setObjectFactory()");
prov.setObjectFactory(objectFactory);
if (fileManagerFactory == null || fileManagerFactory.getFileManager() == null) {
LOG.warn("LoadPackages - FileManagerFactory parameter is null or produces a null FileManager, replacing with a new DefaultFileManagerFactory instance");
final DefaultFileManagerFactory defaultFileManagerFactory = new DefaultFileManagerFactory();
final Container container = getContextContainer();
if (container == null) {
LOG.warn("LoadPackages - Config Container is null. May cause a NPE to be thrown");
} else {
// Apply configuration (including the container reference) to the DefaultFileManagerFactory instance.
container.inject(defaultFileManagerFactory);
}
prov.setFileManagerFactory(defaultFileManagerFactory);
} else {
prov.setFileManagerFactory(fileManagerFactory);
}
LOG.trace("LoadPackages - After prov.setFileManagerFactory(). Before init()");
prov.init(config);
LOG.trace("LoadPackages - After prov.init(). Before loadPackages()");
prov.loadPackages();
LOG.trace("LoadPackages - After prov.loadPackages(). Before config.getPackageConfigs().values()");
List<PackageConfig> list = new ArrayList<>(config.getPackageConfigs().values());
LOG.trace("LoadPackages - After config.getPackageConfigs(). Before pkgConfigs.values()");
list.removeAll(pkgConfigs.values());
return list;
}
use of com.opensymphony.xwork2.FileManager in project struts by apache.
the class Dispatcher method init_FileManager.
private void init_FileManager() throws ClassNotFoundException {
if (initParams.containsKey(StrutsConstants.STRUTS_FILE_MANAGER)) {
final String fileManagerClassName = initParams.get(StrutsConstants.STRUTS_FILE_MANAGER);
final Class<FileManager> fileManagerClass = (Class<FileManager>) Class.forName(fileManagerClassName);
LOG.info("Custom FileManager specified: {}", fileManagerClassName);
configurationManager.addContainerProvider(new FileManagerProvider(fileManagerClass, fileManagerClass.getSimpleName()));
} else {
// add any other Struts 2 provided implementations of FileManager
configurationManager.addContainerProvider(new FileManagerProvider(JBossFileManager.class, "jboss"));
}
if (initParams.containsKey(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY)) {
final String fileManagerFactoryClassName = initParams.get(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY);
final Class<FileManagerFactory> fileManagerFactoryClass = (Class<FileManagerFactory>) Class.forName(fileManagerFactoryClassName);
LOG.info("Custom FileManagerFactory specified: {}", fileManagerFactoryClassName);
configurationManager.addContainerProvider(new FileManagerFactoryProvider(fileManagerFactoryClass));
}
}
use of com.opensymphony.xwork2.FileManager in project struts by apache.
the class JSPLoader method compileJava.
/**
* Compiles the given source code into java bytecode
*/
private void compileJava(String className, final String source, Set<String> extraClassPath) throws IOException {
LOG.trace("Compiling [{}], source: [{}]", className, source);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
// the generated bytecode is fed to the class loader
JavaFileManager jfm = new ForwardingJavaFileManager<StandardJavaFileManager>(compiler.getStandardFileManager(diagnostics, null, null)) {
@Override
public JavaFileObject getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
MemoryJavaFileObject fileObject = new MemoryJavaFileObject(name, kind);
classLoader.addMemoryJavaFileObject(name, fileObject);
return fileObject;
}
};
// read java source code from memory
String fileName = className.replace('.', '/') + ".java";
SimpleJavaFileObject sourceCodeObject = new SimpleJavaFileObject(toURI(fileName), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException, IllegalStateException, UnsupportedOperationException {
return source;
}
};
// build classpath
// some entries will be added multiple times, hence the set
List<String> optionList = new ArrayList<String>();
Set<String> classPath = new HashSet<String>();
// find available jars
ClassLoaderInterface classLoaderInterface = getClassLoaderInterface();
UrlSet urlSet = new UrlSet(classLoaderInterface);
// find jars
List<URL> urls = urlSet.getUrls();
if (urls != null && urls.size() > 0) {
final FileManagerFactory fileManagerFactoryGetInstance = ServletActionContext.getActionContext().getInstance(FileManagerFactory.class);
final FileManagerFactory contextFileManagerFactory = (fileManagerFactoryGetInstance != null ? fileManagerFactoryGetInstance : (FileManagerFactory) ServletActionContext.getActionContext().get(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY));
final FileManagerFactory fileManagerFactory = (contextFileManagerFactory != null ? contextFileManagerFactory : new DefaultFileManagerFactory());
final FileManager fileManagerGetInstance = fileManagerFactory.getFileManager();
final FileManager contextFileManager = (fileManagerGetInstance != null ? fileManagerGetInstance : (FileManager) ServletActionContext.getActionContext().get(StrutsConstants.STRUTS_FILE_MANAGER));
final FileManager fileManager = (contextFileManager != null ? contextFileManager : new DefaultFileManager());
for (URL url : urls) {
URL normalizedUrl = fileManager.normalizeToFileProtocol(url);
File file = FileUtils.toFile(ObjectUtils.defaultIfNull(normalizedUrl, url));
if (file.exists())
classPath.add(file.getAbsolutePath());
}
}
// these should be in the list already, but I am feeling paranoid
// this jar
classPath.add(getJarUrl(EmbeddedJSPResult.class));
// servlet api
classPath.add(getJarUrl(Servlet.class));
// jsp api
classPath.add(getJarUrl(JspPage.class));
try {
Class instanceManager = Class.forName("org.apache.tomcat.InstanceManager");
classPath.add(getJarUrl(instanceManager));
} catch (ClassNotFoundException e) {
// ok ignore
}
// add extra classpath entries (jars where tlds were found will be here)
for (Iterator<String> iterator = extraClassPath.iterator(); iterator.hasNext(); ) {
String entry = iterator.next();
classPath.add(entry);
}
String classPathString = StringUtils.join(classPath, File.pathSeparator);
if (LOG.isDebugEnabled()) {
LOG.debug("Compiling [#0] with classpath [#1]", className, classPathString);
}
optionList.addAll(Arrays.asList("-classpath", classPathString));
// compile
JavaCompiler.CompilationTask task = compiler.getTask(null, jfm, diagnostics, optionList, null, Arrays.asList(sourceCodeObject));
if (!task.call()) {
throw new StrutsException("Compilation failed:" + diagnostics.getDiagnostics().get(0).toString());
}
}
use of com.opensymphony.xwork2.FileManager in project struts by apache.
the class NotURLClassLoader method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
result = new EmbeddedJSPResult();
request = EasyMock.createNiceMock(HttpServletRequest.class);
response = new MockHttpServletResponse();
context = new MockServletContext();
config = new MockServletConfig(context);
final Map params = new HashMap();
HttpSession session = EasyMock.createNiceMock(HttpSession.class);
EasyMock.replay(session);
EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
EasyMock.expect(request.getParameterMap()).andReturn(params).anyTimes();
EasyMock.expect(request.getParameter("username")).andAnswer(() -> ActionContext.getContext().getParameters().get("username").getValue());
EasyMock.expect(request.getAttribute("something")).andReturn("somethingelse").anyTimes();
EasyMock.replay(request);
// mock value stack
ValueStack valueStack = EasyMock.createNiceMock(ValueStack.class);
EasyMock.expect(valueStack.getActionContext()).andReturn(ActionContext.getContext()).anyTimes();
EasyMock.replay(valueStack);
// mock converter
XWorkConverter converter = new DummyConverter();
DefaultFileManager fileManager = new DefaultFileManager();
fileManager.setReloadingConfigs(false);
// mock container
Container container = EasyMock.createNiceMock(Container.class);
EasyMock.expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes();
TextParser parser = new OgnlTextParser();
EasyMock.expect(container.getInstance(TextParser.class)).andReturn(parser).anyTimes();
EasyMock.expect(container.getInstanceNames(FileManager.class)).andReturn(new HashSet<>()).anyTimes();
EasyMock.expect(container.getInstance(FileManager.class)).andReturn(fileManager).anyTimes();
UrlHelper urlHelper = new DefaultUrlHelper();
EasyMock.expect(container.getInstance(UrlHelper.class)).andReturn(urlHelper).anyTimes();
FileManagerFactory fileManagerFactory = new DummyFileManagerFactory();
EasyMock.expect(container.getInstance(FileManagerFactory.class)).andReturn(fileManagerFactory).anyTimes();
EasyMock.replay(container);
ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).withServletRequest(request).withServletResponse(response).withServletContext(context).withContainer(container).withValueStack(valueStack).bind();
}
use of com.opensymphony.xwork2.FileManager in project struts by apache.
the class AnnotationActionValidatorManagerTest method testGetValidatorsForGivenMethodNameWithoutReloading.
public void testGetValidatorsForGivenMethodNameWithoutReloading() throws ValidationException {
FileManager fileManager = container.getInstance(FileManagerFactory.class).getFileManager();
List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias, "execute");
// disable configuration reload/devmode
fileManager.setReloadingConfigs(false);
// 17 in the class level + 0 in the alias
assertEquals(12, validatorList.size());
validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias, "execute");
// expect same number of validators
assertEquals(12, validatorList.size());
}
Aggregations