Setting up Custom class Object as Key/value in Apache Geode - java

I am using Apache Geode in one of my web applications.
I am using Custom class as a value-constraint in Apache Geode cache-server.xml file.
If value-constraint is not specified in the XML file, then I am able to start the server. But if value-constraint is specified then while running the server I am getting below error
Exception in thread "main" org.apache.geode.cache.CacheXmlException: While reading Cache XML file:/C:/localfolder/cache-server.xml. Could not load value-constraint class: myPackage.Test, caused by java.lang.ClassNotFoundException: myPackage.Test
Below is my startserver.bat file contents
gfsh start server --server-port=12000 --dir=C:\localfolder\server
--name=server123 --hostname-for-clients="someHost" --initial-heap=500m --max-heap=500m --bind-address="someAddress" --properties-file=gemfire.properties --server-properties-file=gfsecurity.properties --classpath="C:\localfolder\application.war"
Please let me know
How to provide a custom class objects as a key-value constraint in the XML file?
How to use war file, in the server start process?

The classpath (Gfsh option, and in general, to the java launcher) cannot refer to a WAR file; must be a JAR file (containing your application domain classes).
WAR files and JAR files have similar, but significantly different formats.

Related

Error Reading custom configuration file in dropwizard Application

So we have a normal batching application that we decided to port over to dropwizard. We want to use dropwizard for lot of benefits that it gives us out of the box like HealthCheck, metrics etc.
We might add some REST admin end points later but right now we are just using DropWizard Managed Service to start our application in a separate thread. We created a sample config yml file but it's mostly just a skeleton.
This porting over is just mostly lift and shift and we want to avoid huge refactor in our code. The application is currently tied with two different properties file and we want to keep it that way.
We have figured a way out to deploy our app using a custom deployer that generates these property files "application.properties" and "system.properties"
For some reason the app can't read those files and is throwing NPE. This is a java application. All the jars are under lib directory and the config files are under config directory. It just doesn't seem to resolve those files. I have tried searching the dropwizard docs but didn't find anything useful.
The code just simply expects the file in a particular directory
private static final String APP_PROPERTIES_FILE = "/application.properties";
File file = new File(DataSourceFactory.class.getResource(filename)
.getFile());
The dropwizard version : 1.2.2
And this is how I'm running the app :
java -cp "deployed/App/lib/*:deployed/App/config/*" com.commercehub.app.MainClass server config.yml
Any comments are really appreciated!
The issue was with my classpath. I didn't need the trailing /* for the config folder.
java -cp "deployed/App/lib/*:deployed/App/config" com.commercehub.app.MainClass server config.yml

Reading a XML file from JAR within web application

I am trying to read XML file from JAR used as library in my web app. How can I achieve that? My directory structure for XML file is:
commons/resources/config.xml
where commons is a Java project for which I will create a JAR and place under
apache-tomcat-8.0.32/webapps/myWebApp-1.4/WEB-INF/lib
after building and deploying it.
You can read the XML file as resource from classpath this way: this.getClass().getResourceAsStream("/config.xml"). It returns a stream that you can use for reading the content of the file and do with it whatever you want. It is only possible within the webapp the jar belongs to because webapps are isolated with respect to classloading.

Reading reloadable xml file from external Tomcat directory

Prerequisites
Apache Tomcat 7
Spring 3.2.11.RELEASE
Apache Camel 2.14.1
My Webapplication is deployed in ${catalina.home}/webapps/ as usual
Requirements
Reading xml file placed outside of war-Archive (for example from ${catalina.home}/myfolder/)
The xml file should be reloadable. So if the xml changes the new xml file should be available in my Webapplication
The xml file should be mapped to Java-Objects
First try
I have added the file to classpath via tomcat in catalina.home/conf/catalina.properties:
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.home}/myfolder/
The file is placed here:
${catalina.home}/myfolder/myFile.xml
Reading and mapping the file to Java-Objects works via a timer in apache camel.
from("timer://myTimer?fixedRate=true&period=20000")
.setBody(simple("resource:classpath:myFile.xml"))
.unmarshal(myFileJaxbDataFormat)
.process(myFileTimerProcessor);
myFileTimerProcessor takes the mapped Objects and stores it to a Spring-Bean.
This Bean is used by other Camel-Routes to access the Data contained in the xml file.
Problem
As Claus Ibsen mentioned below the problem is that the ClassLoader caches the file. So the file is not read again if data in it has changed.
If the file ${catalina.home}/myfolder/myFile.xml changes it should be read again and the new values should be available to the timer so it can read the new values for the application.
Is there a possibility to read a xml file outside of war-Archive and reload it if it's content changes?
Is there a more common wary to do that?
Thanks in advance.
Regards,
Max
EDIT 1:
I have restructured the question to ask not only specific details.
The file is cached by the Java classloader, not something Camel can do anything about. You likely need to load the file using file resources instead of classpath.

netbeans and hibernate configuration

How can I load the configuration information for hibernate dynamically from a config file. Netbeans currently hard codes that information into an xml file that is then compiled into the jar. I'm a newbie to Java/Netbeans coming from PHP land and am use to a central bootstrap that pulls from a .ini or something similar, but netbeans tends to hardcode this information upon generation of the models,etc in an xml file that is then compiled in the jar. I'm looking for conventional methods of setting up configuration for various client machines using various database configurations. I don't want to have to compile the app on each machine it must be installed on.
The configuration file is read using the Configuration class. By default, it uses the hibernate.cfg.xml file found in the classpath, but you can use the configure method taking a file as parameter, and store the config file on the file system rather than in the jar.
You can also put the static mapping, which never changes between configs, in a file inside the jar, and put the varying config inside an external file. Look at the javadoc for Configuration to know how to add resources and config files to the configuration.

XSL import causing FileNotFoundException in web application

I have a simple web application under websphere5. Under appDir\WEB-INF\classes\ I have these files:
main.xslt
templates.xslt
main.xslt contains the instruction
<xsl:import href="templates.xsl" />
but the application fails when main.xslt is used in Java code. How should I specify the path to imported XSL files if they all are in the same folder (WEB-INF\classes\)?
Text of exception:
java.io.FileNotFoundException: d:\Program Files\WebSphere\AppServer1\templates.xsl (The system cannot find the file specified.
)
You need to provide a custom uri-resolver to process the includes. In a web application, there's no guarantee that a filesystem is accessible, as you could be running out of a WAR file. Take a look at the javax.xml.transform.URIResolver interface and Transformer.setURIResolver()
Depending on how you loaded the main.xsl, you may need to set the SystemID property, so that it can resolve the relative path.

Categories