コード例 #1
0
String AbstractContextI::loadValueFromFile(const String& fileurl) const throw (Exception)
{
	StringBuffer buf;
	buf->append(fileurl);
	if (0 != buf->indexOf(L"file://")) throw NamingException(WITHDETAILS(L"Expected url starting wtih 'file://' but found : " + fileurl));
	
	bool mustprependworkingdirectory = L'/' != buf->charAt(7);
	
	StringBuffer filename;
	if (L':' == buf->charAt(9)) // windows filepath
	{
		filename->append(buf->substring(8));
		filename = filename->replaceAll(L"/", L"\\");
	}
	else
	{
		filename->append(buf->substring(7));
	}


	String fullpath;
	if (mustprependworkingdirectory)
	{
		Anything value;
		lookup(L"/env/jlj_context_working_directory", value);
		String workingdirectory = value;
		fullpath = workingdirectory + filename;
	}
	else
	{
		fullpath = filename->toString();
	}
	if (verboseOutput()) cout << "JNDI context : Reading from file : " << fullpath << endl;
	InputStream file = new FileInputStreamI(fullpath);
	UTF8String content;
	file->read(content, 0);
	file->close();
	return content->toString();
}
コード例 #2
0
String AbstractContextI::loadValueFromURL(const String& urlstring) const throw (Exception)
{
	String result;
	StringBuffer buf;
	buf->append(urlstring);
	if (0 == buf->indexOf(L"http://"))
	{
		URL url = new URLI(urlstring);
		HttpURLConnection httpurlconnection;
		url->openConnection()->downcast(httpurlconnection);
		if (verboseOutput()) cout << "JNDI context : Get response from url : " << urlstring << endl;
		int rc = httpurlconnection->getResponseCode();
		UTF8StringBuffer cbuf;
#if defined (_WINDOWS_SOURCE)
		cbuf->append(getenv("TEMP"));
		cbuf->append("\\");
		cbuf->append(getenv("USERNAME"));
		cbuf->append(".");
#else
		cbuf->append(getenv("USER_HOME"));
		cbuf->append("/.");
#endif
		cbuf->append(urlstring->toMD5());
		cbuf->append(".properties");
		String cachefilepath = cbuf->toString()->toLowerCase();
		if (200 != rc) 
		{
			if (verboseOutput()) cout << "JNDI context : HTTP status = " << rc << endl;
			if (verboseOutput()) cout << "JNDI context : Trying to load from cached file : " << cachefilepath << endl;
			InputStream file = new FileInputStreamI(cachefilepath);
			UTF8String content;
			file->read(content, 0);
			file->close();
			result = content->toString();
		}
		else
		{
			String response = httpurlconnection->getResponseMessage();
			Writer file = new FileWriterI(cachefilepath);
			file->write(response);
			file->close();
			if (verboseOutput()) cout << "JNDI context : Caching to file : " << cachefilepath << endl;
			result = response;
		}
	}
	else if (0 == buf->indexOf(L"file://"))
	{
		result = loadValueFromFile(urlstring);
	}
	else
	{
		throw NamingException(WITHDETAILS(L"Unsupported URI format: " + urlstring));
	}
	StringBuffer b = result;
	while (-1 < b->indexOf(L"${"))
	{
		InitialContext initialcontext = InitialContext::newInstance();
		int bpos = 0;
		int epos = 0;
		while (bpos < b->length())
		{
			bpos = b->indexOf(L"${", bpos);
			if (-1 == bpos) break;
			epos = b->indexOf(L"}", bpos);
			String variablename = b->substring(bpos + 2, epos);
			StringAnything variablevalue ;
			initialcontext->lookup(L"/" + variablename, variablevalue);
			StringBuffer value = variablevalue->toString(); 
			if (-1 < value->indexOf(L"${" + variablename + L"}") )
				throw Exception(WITHDETAILS(L"Recursive reference found : ${" + variablename + L"}"));
			b->replace(bpos, epos + 1, value->toString());
			bpos = epos + 1;
		}
	}
	return b->toString();
}