use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.
the class XMLConfigWebFactory method loadApplication.
/**
* @param configServer
* @param config
* @param doc
* @throws IOException
* @throws PageException
*/
private static void loadApplication(ConfigServerImpl configServer, ConfigImpl config, Document doc, int mode) throws IOException, PageException {
boolean hasCS = configServer != null;
boolean hasAccess = ConfigWebUtil.hasAccess(config, SecurityManager.TYPE_SETTING);
Element application = getChildByName(doc.getDocumentElement(), "application");
Element scope = getChildByName(doc.getDocumentElement(), "scope");
// Listener type
ApplicationListener listener;
if (mode == ConfigImpl.MODE_STRICT) {
listener = new ModernAppListener();
} else {
listener = ConfigWebUtil.loadListener(getAttr(application, "listener-type"), null);
if (listener == null) {
if (hasCS && configServer.getApplicationListener() != null)
listener = ConfigWebUtil.loadListener(configServer.getApplicationListener().getType(), null);
if (listener == null)
listener = new MixedAppListener();
}
}
String[] strTypes = new String[] { "function", "include", "query", "resource", "http", "file", "webservice" };
int[] types = new int[] { Config.CACHEDWITHIN_FUNCTION, Config.CACHEDWITHIN_INCLUDE, Config.CACHEDWITHIN_QUERY, Config.CACHEDWITHIN_RESOURCE, Config.CACHEDWITHIN_HTTP, Config.CACHEDWITHIN_FILE, Config.CACHEDWITHIN_WEBSERVICE };
// cachedwithin
for (int i = 0; i < types.length; i++) {
String cw = getAttr(application, "cached-within-" + strTypes[i]);
if (!StringUtil.isEmpty(cw, true))
config.setCachedWithin(types[i], cw);
else if (hasCS)
config.setCachedWithin(types[i], configServer.getCachedWithin(types[i]));
}
// Type Checking
Boolean typeChecking = Caster.toBoolean(getAttr(application, "type-checking"), null);
if (typeChecking != null)
config.setTypeChecking(typeChecking.booleanValue());
else if (hasCS)
config.setTypeChecking(configServer.getTypeChecking());
// Listener Mode
int listenerMode = ConfigWebUtil.toListenerMode(getAttr(application, "listener-mode"), -1);
if (listenerMode == -1) {
if (hasCS)
listenerMode = configServer.getApplicationListener() == null ? ApplicationListener.MODE_CURRENT2ROOT : configServer.getApplicationListener().getMode();
else
listenerMode = ApplicationListener.MODE_CURRENT2ROOT;
}
listener.setMode(listenerMode);
config.setApplicationListener(listener);
// Req Timeout URL
if (mode == ConfigImpl.MODE_STRICT) {
config.setAllowURLRequestTimeout(false);
} else {
String allowURLReqTimeout = getAttr(application, "allow-url-requesttimeout");
if (hasAccess && !StringUtil.isEmpty(allowURLReqTimeout)) {
config.setAllowURLRequestTimeout(Caster.toBooleanValue(allowURLReqTimeout, false));
} else if (hasCS)
config.setAllowURLRequestTimeout(configServer.isAllowURLRequestTimeout());
}
// Req Timeout
TimeSpan ts = null;
if (hasAccess) {
String reqTimeoutApplication = getAttr(application, "requesttimeout");
// deprecated
String reqTimeoutScope = getAttr(scope, "requesttimeout");
if (!StringUtil.isEmpty(reqTimeoutApplication))
ts = Caster.toTimespan(reqTimeoutApplication);
if (ts == null && !StringUtil.isEmpty(reqTimeoutScope))
ts = Caster.toTimespan(reqTimeoutScope);
}
if (ts != null && ts.getMillis() > 0)
config.setRequestTimeout(ts);
else if (hasCS)
config.setRequestTimeout(configServer.getRequestTimeout());
// script-protect
String strScriptProtect = getAttr(application, "script-protect");
if (hasAccess && !StringUtil.isEmpty(strScriptProtect)) {
// print.err("sp:"+strScriptProtect);
config.setScriptProtect(AppListenerUtil.translateScriptProtect(strScriptProtect));
} else if (hasCS)
config.setScriptProtect(configServer.getScriptProtect());
// classic-date-parsing
if (config instanceof ConfigServer) {
if (mode == ConfigImpl.MODE_STRICT) {
DateCaster.classicStyle = true;
} else {
String strClassicDateParsing = getAttr(application, "classic-date-parsing");
if (!StringUtil.isEmpty(strClassicDateParsing)) {
DateCaster.classicStyle = Caster.toBooleanValue(strClassicDateParsing, false);
}
}
}
// Cache
Resource configDir = config.getConfigDir();
String strCacheDirectory = application.getAttribute("cache-directory");
if (hasAccess && !StringUtil.isEmpty(strCacheDirectory)) {
strCacheDirectory = ConfigWebUtil.translateOldPath(strCacheDirectory);
Resource res = ConfigWebUtil.getFile(configDir, strCacheDirectory, "cache", configDir, FileUtil.TYPE_DIR, config);
config.setCacheDir(res);
} else {
config.setCacheDir(configDir.getRealResource("cache"));
}
String strMax = getAttr(application, "cache-directory-max-size");
if (hasAccess && !StringUtil.isEmpty(strMax)) {
config.setCacheDirSize(ByteSizeParser.parseByteSizeDefinition(strMax, config.getCacheDirSize()));
} else if (hasCS)
config.setCacheDirSize(configServer.getCacheDirSize());
// admin sync
ClassDefinition asc = getClassDefinition(application, "admin-sync-", config.getIdentification());
if (!asc.hasClass())
asc = getClassDefinition(application, "admin-synchronisation-", config.getIdentification());
if (hasAccess && asc.hasClass()) {
try {
Class clazz = asc.getClazz();
if (!Reflector.isInstaneOf(clazz, AdminSync.class))
throw new ApplicationException("class [" + clazz.getName() + "] does not implement interface [" + AdminSync.class.getName() + "]");
config.setAdminSyncClass(clazz);
} catch (Exception e) {
SystemOut.printDate(e);
}
} else if (hasCS)
config.setAdminSyncClass(configServer.getAdminSyncClass());
}
use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.
the class CFMLEngineImpl method deployBundledExtension.
private void deployBundledExtension(ConfigServerImpl cs) {
Resource dir = cs.getLocalExtensionProviderDirectory();
List<ExtensionDefintion> existing = DeployHandler.getLocalExtensions(cs);
Map<String, ExtensionDefintion> existingMap = new HashMap<String, ExtensionDefintion>();
{
Iterator<ExtensionDefintion> it = existing.iterator();
ExtensionDefintion ed;
while (it.hasNext()) {
ed = it.next();
try {
existingMap.put(ed.getSource().getName(), ed);
} catch (ApplicationException e) {
}
}
}
Log log = cs.getLog("deploy");
// get the index
ClassLoader cl = CFMLEngineFactory.getInstance().getCFMLEngineFactory().getClass().getClassLoader();
InputStream is = cl.getResourceAsStream("extensions/.index");
if (is == null)
is = cl.getResourceAsStream("/extensions/.index");
if (is == null)
is = SystemUtil.getResourceAsStream(null, "/extensions/.index");
if (is == null) {
log.error("extract-extension", "could not found [/extensions/.index] defined in the index in the lucee.jar");
return;
}
try {
String index = IOUtil.toString(is, CharsetUtil.UTF8);
log.info("extract-extension", "the following extensions are bundled with the lucee.jar [" + index + "]");
String[] names = lucee.runtime.type.util.ListUtil.listToStringArray(index, ';');
String name;
Resource temp = null;
RHExtension rhe;
ExtensionDefintion exist;
Iterator<ExtensionDefintion> it;
for (int i = 0; i < names.length; i++) {
name = names[i];
if (StringUtil.isEmpty(name, true))
continue;
name = name.trim();
// does it already exist?
if (existingMap.containsKey(name)) {
continue;
}
is = cl.getResourceAsStream("extensions/" + name);
if (is == null)
is = cl.getResourceAsStream("/extensions/" + name);
if (is == null) {
log.error("extract-extension", "could not found extension [" + name + "] defined in the index in the lucee.jar");
continue;
}
try {
temp = SystemUtil.getTempDirectory().getRealResource(name);
ResourceUtil.touch(temp);
Util.copy(is, temp.getOutputStream(), false, true);
rhe = new RHExtension(cs, temp, false);
ExtensionDefintion alreadyExists = null;
it = existing.iterator();
while (it.hasNext()) {
exist = it.next();
if (exist.equals(rhe)) {
alreadyExists = exist;
break;
}
}
String trgName = rhe.getId() + "-" + rhe.getVersion() + ".lex";
if (alreadyExists == null) {
temp.moveTo(dir.getRealResource(trgName));
log.info("extract-extension", "added [" + name + "] to [" + dir + "]");
} else if (!alreadyExists.getSource().getName().equals(trgName)) {
log.info("extract-extension", "rename [" + alreadyExists.getSource() + "] to [" + trgName + "]");
alreadyExists.getSource().moveTo(alreadyExists.getSource().getParentResource().getRealResource(trgName));
}
// now we check all extension name (for extension no longer delivered by lucee)
it = existing.iterator();
while (it.hasNext()) {
exist = it.next();
trgName = exist.getId() + "-" + exist.getVersion() + ".lex";
if (!trgName.equals(exist.getSource().getName())) {
exist.getSource().moveTo(exist.getSource().getParentResource().getRealResource(trgName));
log.info("extract-extension", "rename [" + exist.getSource() + "] to [" + trgName + "]");
}
}
} finally {
if (temp != null && temp.exists())
temp.delete();
}
}
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
log.error("extract-extension", t);
}
return;
}
use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.
the class CFMLEngineImpl method _service.
private void _service(HttpServlet servlet, HttpServletRequest req, HttpServletResponse rsp, short type) throws ServletException, IOException {
CFMLFactoryImpl factory = (CFMLFactoryImpl) getCFMLFactory(servlet.getServletConfig(), req);
// is Lucee dialect enabled?
if (type == Request.TYPE_LUCEE) {
if (!((ConfigImpl) factory.getConfig()).allowLuceeDialect()) {
try {
PageContextImpl.notSupported();
} catch (ApplicationException e) {
throw new PageServletException(e);
}
}
}
boolean exeReqAsync = exeRequestAsync();
PageContextImpl pc = factory.getPageContextImpl(servlet, req, rsp, null, false, -1, false, !exeReqAsync, false, -1, true, false, false);
try {
Request r = new Request(pc, type);
if (exeReqAsync) {
r.start();
long ended = -1;
do {
SystemUtil.wait(Thread.currentThread(), 1000);
// done?
if (r.isDone()) {
// print.e("mas-done:"+System.currentTimeMillis());
break;
} else // reach request timeout
if (ended == -1 && (pc.getStartTime() + pc.getRequestTimeout()) < System.currentTimeMillis()) {
// print.e("req-time:"+System.currentTimeMillis());
CFMLFactoryImpl.terminate(pc, false);
ended = System.currentTimeMillis();
// break; we do not break here, we give the thread itself the chance to end we need the exception output
} else // the thread itself seem blocked, so we release this thread
if (ended > -1 && ended + 10000 <= System.currentTimeMillis()) {
// print.e("give-up:"+System.currentTimeMillis());
break;
}
} while (true);
} else // run in thread coming from servlet engine
{
try {
Request.exe(pc, type, true, false);
} catch (RequestTimeoutException rte) {
if (rte.getThreadDeath() != null)
throw rte.getThreadDeath();
} catch (NativeException ne) {
if (ne.getCause() instanceof ThreadDeath)
throw (ThreadDeath) ne.getCause();
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t) {
}
}
} finally {
factory.releaseLuceePageContext(pc, !exeReqAsync);
}
}
use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.
the class CFMLEngineImpl method getConfigDirectory.
/**
* loads Configuration File from System, from init Parameter from web.xml
*
* @param sg
* @param configServer
* @param countExistingContextes
* @return return path to directory
*/
private Resource getConfigDirectory(ServletConfig sg, ConfigServerImpl configServer, int countExistingContextes, RefBoolean isCustomSetting) throws PageServletException {
isCustomSetting.setValue(true);
ServletContext sc = sg.getServletContext();
String strConfig = sg.getInitParameter("configuration");
if (StringUtil.isEmpty(strConfig))
strConfig = sg.getInitParameter("lucee-web-directory");
if (StringUtil.isEmpty(strConfig))
strConfig = System.getProperty("lucee.web.dir");
if (StringUtil.isEmpty(strConfig)) {
isCustomSetting.setValue(false);
strConfig = "{web-root-directory}/WEB-INF/lucee/";
} else // only for backward compatibility
if (strConfig.startsWith("/WEB-INF/lucee/"))
strConfig = "{web-root-directory}" + strConfig;
strConfig = StringUtil.removeQuotes(strConfig, true);
// static path is not allowed
if (countExistingContextes > 1 && strConfig != null && strConfig.indexOf('{') == -1) {
String text = "static path [" + strConfig + "] for servlet init param [lucee-web-directory] is not allowed, path must use a web-context specific placeholder.";
System.err.println(text);
throw new PageServletException(new ApplicationException(text));
}
strConfig = SystemUtil.parsePlaceHolder(strConfig, sc, configServer.getLabels());
ResourceProvider frp = ResourcesImpl.getFileResourceProvider();
Resource root = frp.getResource(ReqRspUtil.getRootPath(sc));
Resource res;
Resource configDir = ResourceUtil.createResource(res = root.getRealResource(strConfig), FileUtil.LEVEL_PARENT_FILE, FileUtil.TYPE_DIR);
if (configDir == null) {
configDir = ResourceUtil.createResource(res = frp.getResource(strConfig), FileUtil.LEVEL_GRAND_PARENT_FILE, FileUtil.TYPE_DIR);
}
if (configDir == null && !isCustomSetting.toBooleanValue()) {
try {
res.createDirectory(true);
configDir = res;
} catch (IOException e) {
throw new PageServletException(Caster.toPageException(e));
}
}
if (configDir == null) {
throw new PageServletException(new ApplicationException("path [" + strConfig + "] is invalid"));
}
if (!configDir.exists() || ResourceUtil.isEmptyDirectory(configDir, null)) {
Resource railoRoot;
// there is a railo directory
if (configDir.getName().equals("lucee") && (railoRoot = configDir.getParentResource().getRealResource("railo")).isDirectory()) {
try {
copyRecursiveAndRename(railoRoot, configDir);
} catch (IOException e) {
try {
configDir.createDirectory(true);
} catch (IOException ioe) {
}
return configDir;
}
// zip the railo-server di and delete it (optional)
try {
Resource p = railoRoot.getParentResource();
CompressUtil.compress(CompressUtil.FORMAT_ZIP, railoRoot, p.getRealResource("railo-web-context-old.zip"), false, -1);
ResourceUtil.removeEL(railoRoot, true);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
} else {
try {
configDir.createDirectory(true);
} catch (IOException e) {
}
}
}
return configDir;
}
use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.
the class RHExtension method toBundleDefinition.
private static BundleDefinition toBundleDefinition(InputStream is, String name, String extensionVersion, boolean closeStream) throws IOException, BundleException, ApplicationException {
Resource tmp = SystemUtil.getTempDirectory().getRealResource(name);
try {
IOUtil.copy(is, tmp, closeStream);
BundleFile bf = new BundleFile(tmp);
if (bf.isBundle())
throw new ApplicationException("Jar [" + name + "] is not a valid OSGi Bundle");
return new BundleDefinition(bf.getSymbolicName(), bf.getVersion());
} finally {
tmp.delete();
}
}
Aggregations