ClassLoader::getSystemResource returning null - kotlin

It used to work in the past, but I don't what happened in the middle, now it returns always null.
The file to read is in the project root diretory, which corresponds to the output of Paths.get(".").
Note: function is top-level
I'm reading the imgui.ini file here
fileLoadToLines(iniFilename)
where it's so defined
fun fileLoadToLines(filename: String) = ClassLoader.getSystemResourceAsStream(filename)?.use { it.bufferedReader().readLines() }
Tried also the other Thread.currentThread().contextClassLoader, no success
What's the problem?

The project root directory is typically the default current working directory, but not necessarily on the classpath. That's why Paths.get(".") returns it, while the classloader doesn't find the file under it, because the latter goes by what's in the classpath.
It used to work probably because you had the project root added to the runtime classpath. The solution I would recommend is instead of using a classloader, just use the file system API in java.io to load it.

Related

Xquery extracting property values from .properties file

I am currently trying to extract property values from my properties file, but am running into some problems. I can't test this in ML query console, because the properties file doesn't exist there. I am currently trying to grab the values of the file like this
let $port := #{#properties["ml.properties-name"]}
I've also looked at
xdmp:document-get-properties(
$uri as xs:string,
$property as xs:QName
however that is limited to .xml files I believe. Does anyone have a way/work-around of accessing these values? I can't seem to find one I've looked at some documentation on Marklogic's website, but can't seem to get anything to work. The way I was accessing before was in ruby, through monkey-patching allowing me to access those private fields.The problem with that is the ruby script I call is only called once, while my .xqy file is ran every minute that sends args to another function. I need to access those args from the properties file, right now I just have them hard-coded in. Any thoughts?
Thanks
You cannot access deployment properties like that, but you can pass them along with deployment. If you create a new REST app with latest Roxy, you should get a copy of this config.xqy added to src/config/:
https://github.com/marklogic-community/roxy/blob/master/deploy/sample/custom-config.xqy
That file is treated specially when deployed to the modules database. Properties references are replaced inside there. In your case, add another variable, and give it a string value following the #ml.xyz pattern:
declare variable $c:port := "#ml.property-name";
You can then import the config lib, and use it in your code.
These so-called Deployer Substitutions are described in more detail on the Roxy wiki:
https://github.com/marklogic-community/roxy/wiki/Deployer-Substitutions

Carrot2-bisectingKmeans setting the attribute of resouceLookup causes the error of no resouces named stopwords.ar

Carrot2-I tried to cluster docs through the bisectingKmeans algorithm.
I set the attribute of resouceLookup and tested the language of English/Chinese. It both resulted in the error "no resouces named stopwords.ar in resouce lookup locations...".
The cod I wrote:
//set the resoucelookup
File resDir = new File("resouces");
ResouceLookup res = new ResouceLookup(new DirLocator(resDir));
LexicalDataLoaderDescriptor.attributeBuilder(preprocessAttr).resourceLookup(res);
//set the language
MultilingualClusteringDescriptor.attributeBuilder(processingAttr)
.defaultLanguage(LanguageCode.CHINESE_SIMPLIFIED);
What's the problem? Thanks.
Copying all the resources is a better idea and the reason why there is no default fallback -- you will have complete control over what resources are present and for which language. This is important because resources are merged by default http://download.carrot2.org/head/manual/index.html#section.attribute.kmeans.merge-resources
The most explicit way to solve this would be to copy all all lexical resources to your resources directory. You can find the resource files in Carrot2 Java API distribution, for example.
An alternative is to tell Carrot2 to load the missing resources from the classpath (Carrot2 JAR):
File resourcesDir = new File("resources");
ResourceLookup resourceLookup = new ResourceLookup(
new DirLocator(resourcesDir), // your custom location
new ContextClassLoaderLocator() // fallback: classpath (Carrot2 JAR)
);
LexicalDataLoaderDescriptor.attributeBuilder(attrs)
.resourceLookup(resourceLookup);
In this arrangement, your resources directory can only contain the resources you'd like to override. The non-overridden ones (and the ones that you don't care about) will be loaded from Carrot2 JAR.
There are two caveats here:
When you upgrade Carrot2 JAR, the fallback resources may silently change.
In case of a misconfigured custom location (e.g. passing an empty directory), clustering will silently proceed with built-in lexical resources without any specific warning (unless you use the debug logging level).

Resource Files in CF - Not Embedded

I have a PPC2003 project in VS2005. I have added a resource file (SomeResources.resx) to the project. I can access the test string I have in the file by using My.Resources.SomeResources.MyTestString (I am using the default Custom Tool Name that VS provides).
When the Build Action property of the is set to Embedded Resource, the application references the MyTestString successfully.
But I do not want to embed the file, so that it's string values can be modified after it has been deployed/installed.
I, therefore, changed the Build Action to Content, so that the file gets copied out to the device for potential future manipulation. When I call MyTestString I get the following error:
MissingManifestResourceException Stack Trace: at System.Resources.ResourceManager.InternalGetResourceSet() at System.Resources.ResourceManager.InternalGetResourceSet() at System.Resources.ResourceManager.InternalGetResourceSet() at System.Resources.ResourceManager.GetString() at MyApp.My.Resources.SomeResources.get_MyTestString() at MyApp.fMain.fMain_Load() at System.Windows.Forms.Form.OnLoad() at System.Windows.Forms.Form._SetVisibleNotify() at System.Windows.Forms.Control.set_Visible() at System.Windows.Forms.Application.Run() at MyApp.fMain.Main()
As the file is not embedded, do I maybe need to manually load it first? If so, how? Any other ideas? Is it not possible to do what I'm after achieving and should I just create my own XML file/reader?
Resources (resx files) are specifically designed to be compiled into the application. If you want it to be an editable content file on the target, then you have to approach it differently and use something like an XML file and wrap that with accessors (akin to the Configuration namespace stuff in the full framework).

How to update the JSF2.0 (Primefaces) tooltips dynamically without server restart

I need to update the JSF2.0 (Primefaces) tooltips dynamically without server restart.
Meaning need to find a way where tooltips (atm from properties file) of the a running application can be changed without requiring a server restart.
We are running websphere and deploying a non exploded EAR (can probably convince to deploy exploded war)
Any Ideas or tips please. Thanks you
The value attribute of the p:toolTip component must be an EL expression or a literal text. Usually, one would reference a resource bundle declared using the var attribute of the f:loadBundle tag, in the EL expression for the tooltip.
The underlying resource bundle declared using the basename attribute could be backed by a property file itself (in which case you need to place the property file in the appropriate directory on the classpath), or for that matter it could be a custom ResourceBundle implementation that could read from a properties file (located outside the container), or a database or any store for that matter.
You could therefore change your existing EL expression from the existing one defined as:
<f:loadBundle var="msg" basename="propfile_location" />
to
<f:loadBundle var="msg" basename="fully qualified class name of the ResourceBundle class" />
In simpler words, you will need to roll your own ResourceBundle class(es) to support the various locales. Needless to state, but you will need to override the ResourceBundle.getObject(java.lang.String) method, as it is invoked by the ResourceBundleELResolver implementation when evaluating the EL expressions referencing ResourceBundles.
Additionally, you will need to ensure that the ResourceBundle.getObject(java.lang.String) implementation of your ResourceBundle will always re-fetch and return the value corresponding to the provided key. Failure to ensure this would mean that the initial value fetched by the resource bundle may be returned on subsequent invocations, especially if you are caching the initial value. You are likely to encounter this behavior even if you deploy an exploded WAR file where you can modify the property file contents without a redeployment of the application, and that is why it is important to use a custom ResourceBundle implementation that does not cache values.

Best way to use groovy scripts in an application

I'm trying to use groovy scripts in my application. The problem is that GroovyScriptEngine#run always compiles the script, even if it was compiled in previous runs and hadn't changed since. Even if I set a physical output folder to save compilation results in the configuration.
What is the best way of working around this? The optimum for me is that I'm able to send the script with a folder containing precompiled results and no compilation is done (unless the script is modified of course)
Grails 1.3.5 is using Groovy 1.7.5. In that Groovy version, GroovyScriptEngine.run(..) calls the following methods: createScript(String, Binding) --> loadScriptByName(String) --> isSourceNewer(ScriptCacheEntry).
isSourceNewer(ScriptCacheEntry) is defined as (unfortunately, I didn't find a matching source file on the web):
protected boolean isSourceNewer(ScriptCacheEntry entry)
throws ResourceException {
// ...
for (String scriptName : entry.dependencies) {
// ...
return true; // without any further condition!
}
return false;
}
Which implements the (queer) logic "if a script has dependencies, it is newer than the cached script (and needs to be re-compiled)". That's not what the code is supposed to do; it's supposed to decide by modification time.
In newer versions of GroovyScriptEngine, this has been corrected (there've been massive changes to the logic), but for now, you'd need to subclass GroovyScriptEngine and overwrite isSourceNewer(ScriptCacheEntry) to fix the logic yourself.
Edit: The bug has been reported and fixed in Groovy 1.7.6. - So try using Groovy 1.7.6 in your Grails lib folder.
The solution (hack) I used at last was to stream out the scriptCache variable using xstream and to read it back and set it in the object
Not sure if this helps you, but you can alter GroovyScriptEngine's behaviour using CompilerConfiguration (see GroovyScriptEngine.setConfig). There's an option CompilerConfiguration.setRecompileGroovySource, which can be used to set whether the sources will be reloaded and recompiled if they change. You can read more about CompilerConfiguration here (page 282).