I am using the reflections package to scan some files in my classpath. The code below is run at regular intervals. Basically i am retriving all xml files within a folder 'tmp' in the classpath. This code is being run from a web application.
Reflections reflections = new Reflections("tmp", new ResourcesScanner());
Set<String> xmls = reflections.getResources(Pattern.compile(".*\\.xml"));
When i add another xml file in the services folder it gets read. However, when i update any of those xml files, the update is NOT reflected. Is there some kind of caching going on there? If yes, is there anyway to avoid the same?
No, Reflections does not cache resources. It simply scans and creates a Store object - actually a multimap of Strings.
Reading / retrieving the resources on runtime is done based on their names (via Store) and uses the configured classloader (which defaults to the context class loader)
Related
I am trying to achieve globalization in a .NET class library. In a .NET web application this seems to work fine. I can add multiple resource files under the App_GlobalResources.
e.g.
LocalizedText.resx
LocalizedText.fr.resx
However resource files are handled differently in Class Libraries. I add a resource file by
1) Opening the Properties for the Class Library
2) Clicking on the Resources Tab
3) Clicking on the link to create a “default resources file”
With this model it seems to only want to allow one default resources file. I can rename files and seem to get around this “one file” limitation but if I produce resource files with the same names as above there appears to be no code generated for the “LocalizedText.fr.resx” file. If I reference a string in code like so…
myControl.Text = Properties.LocalizedText.MyLocalizedText;
It references the LocalizedText.resx file for the value (ignoring the fact that I have the culture set to French). I’m guessing that whatever auto-generates the code for the designer file sees that there is already a “LocalizedText” class and doesn’t generate the necessary code.
Is there not a way (equivalent to the web application project) that I can use multiple resource files in a Class Library, named differently for each culture, and be able to easily access this in code (trusting .NET to switch appropriately depending on the culture info)?
Many Thanks
First: create a folder in your project named 'MyFolder'.
Second: add a resource file named 'MyResourceFile' to the folder.
Third: where you want access the resource values, import:
using System.Reflection;
using System.Resources;
using MyProject.MyFolder.MyResourceFile;
and create a property that can access the your resource. Like this:
public static ResourceManager oResourceManager = new ResourceManager("MyProject.MyFolder.MyResourceFile", typeof(MyResourceFile).Assembly);
Fourth: Get the data from your property:
oResourceManager.GetString("ResourceKey", System.Globalization.CultureInfo.CurrentCulture);
I am using resource file to read the resources from.I am using LINQ to read the resources i.e. First of all I am loading the resource xml file in the XDocument and then querying the xml through LINQ to get the resource value.The problem is that everytime I need to read the resource value I have to load the resource file again and again i.e. the following line of code gets executed everytime:
XDocument resourceXML = XDocument.Load("path to resource file");
This in turn has an impact on the performance of application.
What I need is that I want the resource XML to be stored in CACHE and every time I need to read the XML to get the resource value I can read it from cache itself instead of time and again loading the .resx file.This I know will definately improve the performance of my application.
Any help will be greatly appreciated.
I am using resource file to read the resources from.I am using LINQ to
read the resources i.e. First of all I am loading the resource xml
file in the XDocument and then querying the xml through LINQ to get
the resource value
And you are doing in order to read a value stored in a resource file this instead of simply using the autogenerated strongly typed class: ResourceName.SomeResourceKey?
And if your answer is yes I have my reasons, then you may take a look at the System.Runtime.Caching namespace which contains all you need in order to cache data in a .NET application. For example there's the MemoryCache class. The System.Runtime.Caching assembly has been added in .NET 4.0 so if you are using older versions of the framework you could use the ASP.NET Cache.
I currently hold my resources in a database instead of resx files. I would like to be able to test whether all keys used in the application are held in the database to avoid runtime errors.
I have a custom ResourceProviderFactory to accomplish retrieving the resources.
Resources could be in aspx view pages - GetLocalResourceObject("ResourceKey") or GetGlobalResourceObject("ResourceClass", "ResourceKey").
They could be in a controller - HttpContext.GetGlobalResourceObject("ResourceClass", "ResourceKey").
I also call the resources from the core assemblies of my application without using the ASP.NET resource factory (using a singleton instead) - CoreResources.Current.GetResource("ResourceClass", "ResourceKey")
What would be the best way for me to ensure that all resources are in the database without having to waiting for a runtime exception?
The only approach I can think of is writing an app that scans your source code files and extracts all the resource keys you are using. You can then check the existence of each key in your database.
Unfortunately it is not so trivial... I don't think there is an easier way.
I would however change your ResourceProviderFactory to not throw an exception in case of a missing key. Just return some text like "Resource missing: 'xxx'" and log it so that it can be added.
I have a large directory structure with JavaScript, images, etc. that depend on each other. I would like to encapsulate it all into a DLL so I only have to reference one thing and not have multiple copies of all these files across projects.
Because the files depend on each other, I'm thinking I can create an IHttpModule that registers a route to accept URLs such as /MyEmbeddedDir/subdir/file.js. Anything in MyEmbeddedDir would then be handled by a custom IHttpHandler that does the correct mapping. Each web application would then need to reference the DLL and add the module and handler to web.config. Does this seem reasonable?
Also, is there an easier way to embed/reference the files than to set the build action to embedded resource and add [assembly: WebResource(...)] to each file (there are dozens!)? Thanks!
Edit: If I'm not using WebResource.axd then I shouldn't need to add [assembly: WebResource(...)]
Yes, having a single container is a great way to manage large number of files (and no, SQLite won't help here! ;).
We have a product, named SolFS, which is a virtual file system, that lets you keep your data in custom storage (resource DLL is one of the options) and provides file API for accessing the files. We even implemented asynchronous pluggable protocol for IE (on the client side, but the task is very similar to yours). SolFS includes a manager application that lets you easily create container files and import files into container.
I ended up going with the IHTTPModule (register route) and IHTTPHandler (obtain embedded resource). The route is configurable in web.config in case it conflicts with existing content.
Hi Java programmers. I badly need your help.
I have a JavaFX/Java ME application. I'm trying to modify an XML file inside my project's folder (soon to be packaged jar file).
The path of the file I want to write: /parseExample/service1.xml
Sadly, my application is a JavaFx/JavaME so it doesn't contain the library java.util.jar.
So I can't use the jar classes.
Are there other ways to do that?
Even in J2SE you should consider a Jar file as read only. In J2SE the only way to do it is either using external tools or recreating a new Jar file and overwriting the old one with the new.
My recommendation would be to find a storage location as listed below and use your XML file as a default, if you need to change the just put the new one in the storage location.
Your data storage options are the following:
As per http://developer.android.com/guide/topics/data/data-storage.html These are the possible storage locations you have available:
Shared Preferences - Store private primitive data in key-value pairs.
Internal Storage - Store private data on the device memory.
External Storage - Store public data on the shared external storage.
SQLite Databases - Store structured data in a private database.
Network Connection - Store data on the web with your own network server.