Bean configuration for Apache Extras Cassandra-JDBC - spring

I looked around their site http://code.google.com/a/apache-extras.org/p/cassandra-jdbc/ but couldn't find any documentation for configuring a data source bean such as
<spring:bean id="mySqlDataSource"
class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource">
<spring:property name="url"
value="jdbc:mysql://${mysql.host}:${mysql.port}/${mysql.database}?user=${mysql.user}&password=${mysql.pwd}" />
</spring:bean>
Is there any documentation anywhere or does anyone know how to write one?

The CassandraDataSource doesn't seem to have a default constructor, so you'll need to pass everything in:
<spring:bean id="cassDataSource"
class="org.apache.cassandra.cql.jdbc.CassandraDataSource">
<constructor-arg value="host">
<constructor-arg type="int" value="port">
<constructor-arg value="keyspace">
<constructor-arg value="user">
<constructor-arg value="password">
<constructor-arg value="version">
</spring:bean>
(I haven't tested this, but it's basically what you'd need to do).

I found this on Google, and the info is slightly out of date, so I thought I'd update it. The arguments have changed. The best way I found to figure out what arguments it's expecting is to use the source, which you can get off of GitHub using an address like:
https://code.google.com/a/apache-extras.org/p/cassandra-jdbc/source/browse/src/main/java/org/apache/cassandra/cql/jdbc/CassandraDataSource.java?name=v1.2.5
There's a selector on that page where you can pick the version number you downloaded (that link brings you to 1.2.5). You need to satisfy all the constructor arguments.

Related

How to define a PropertyPlaceholderConfigurer local to a specific bean?

I've been using org.springframework.beans.factory.config.PropertyPlaceholderConfigurer and in my experience ("citation needed" LOL) it sets the property values globally.
Is there a way to specify different PropertyPlaceholderConfigurer instances for different beans within the same application context xml?
My current code is similar to
<bean id="a" class="X">
<property name="foo" value="bar"/>
<property name="many" value="more"/>
</bean>
<bean id="b" class="X">
<property name="foo" value="baz"/>
<property name="number_of_properties" value="a zillion"/>
</bean>
I would like to do something like (pseudo-code below):
<bean id="a" class="X">
... parse the contents of "a.properties" here ...
</bean>
<bean id="b" class="X">
... parse the contents of "b.properties" here ...
</bean>
The above is non-working pseudo code to illustrate the concept; the point being, I want a different properties file to feed each bean.
WHY?
I want to have those specific properties in separate properties file and not in XML.
I think the following link can br helpful to you.
Reference Link
where #Value("${my.property.name}") annotation is used to bind the property file to a variable of type Properties which will reside in your bean class where you intend to use that properties file.
and you can define multiplte proprtiesplaceholder as below:
<bean id="myProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:my.properties</value>
</list>
</property>
</bean>
and use the id as reference in your bean variable to initialize properties file to the bean.
And it will be handy to include with placeholder bean.
Kindly refer Importance of Unresolvable Placeholder link for detailed info regarding its usage.
Hope this was helpful.

Ehcache statistics

I would like to see all statistics for the Ehcache when I have running server.
In the documentation I have found objects such as "StatisticsGateway" and "SampledCache". I'm using ehcache 2.9.
By using StatisticsGateway gets incomplete statistics. When using the SampledCache object I get more statistics, but nowhere is described in some way to retrieve the object.
For example, getting the StatisticsGateway object is as follow:
Cache cache = cacheManager.getCache("name");
StatisticsGateway statistic = cache.getStatistics();
statistic.cacheHitCount() etc.
How to get the SampledCache object?
Thanks in advance!
Late answer :) it may help some one else.
You can use jconsole.exe from your java/bin directory. The Jconsole can show you the statistics.
You may need to add the JMX support to see the statistics in Jconsole
<!-- JMX for Ehcache -->
<bean id="managementService" class="net.sf.ehcache.management.ManagementService"
init-method="init" destroy-method="dispose">
<constructor-arg ref="ehcache" />
<constructor-arg ref="mbeanServer" />
<constructor-arg index="2" value="true" />
<constructor-arg index="3" value="true" />
<constructor-arg index="4" value="true" />
<constructor-arg index="5" value="true" />
</bean>
SampleCache acts as a decorator object. Basically you create an instance of SampledCache and pass a Cache instance as a backing cache. The backing Cache is the cache for which you need stats, in your case the cache instance. Something like
SampledCache sampledCache = new SampledCache(cache);
You can call methods on sampledCache to get desired stats. Created a simple example here http://www.ashishpaliwal.com/blog/2015/01/using-ehcache-sampledcache/

How to get Spring Data to read documents from Mongo that don't have _class attributes

MongoTemplate inserts an attribute named "_class" into anything it puts into Mongo, and there are ways to turn that off.
However, it seems to be unwilling to read anything back out of it that doesn't have a _class attribute. Simply removing that from the mongo document appears to make it inaccessible. Since reading data you didn't write yourself is an obvious use case, I figure I must be missing something here.
I've been attempting to use this:
List<SomeClass> list = mongoTemplate.findAll(SomeClass.class, "someCollection");
...where SomeClass is annotated with #Id and #Document, and the documents in someCollection otherwise correctly map to the object. I can verify this by creating one of these objects in code, using insert to get it into Mongo, and then see that I can read it back out again.
This works just fine if _class is there but fails if it is not. I do not care about polymorphism or anything that might actually need this attribute. How can I get MongoTemplate to read data that it didn't itself write?
Okay, I found the answer...DavidA's advice was correct but missing the crucial part: when you set up the MappingMongoConverter dealio with the null type mapping business, it not only stops writing the "_class" pollution, but also stops attempting to read it. This causes it to fall back to the type you provide when attempting to retrieve your documents from Mongo.
I haven't seen anywhere that anyone actually mentions that. :)
So, for anybody else running into this issue, here's the XML configuration I used (adapted from something I found somewhere else here on StackOverflow, but I lost the link, sorry):
<mongo:db-factory id="mongoDbFactory" host="${mongo.host}" port="${mongo.port}" dbname="${mongo.dbname}"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mongoConverter" />
<property name="writeResultChecking" value="EXCEPTION" />
</bean>
<bean id="mongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id="mongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mongoMappingContext" />
<property name="typeMapper" ref="mongoTypeMapper"></property>
</bean>
And then in the Java code:
//build query object
UnifiedProduct mpp = mongoTemplate.findOne(query, UnifiedProduct.class, "collection-name");
...which results in the UnifiedProduct class I wanted, and no annoying "_class" pollution.
Spring's MappingMongoConverter uses a MongoTypeMapper in order to figure out what type to use when reading in a DBObject from the database. The DefaultMongoTypeMapper uses the "_class" attribute to work as you have described.
You should be able to implement your own MongoTypeMapper and tell the MappingMongoConberter to use it. Your version can use other indicators instead of "_class" value to determine what type should be created when reading.

How to refer to the returned type

I want to create a spring bean as below.
<bean id="qNameString" class="javax.xml.xpath.XPathConstants.STRING"/>
Here I want the reference to return type which is a QName but I understand the way I referred is wrong. Can someone please help on this.
That won't work, because class="javax.xml.xpath.XPathConstants.STRING" makes no sense, since what you're referring to isn't a class.
You can refer to static fields using <util:constant>, as documented here:
<property name="...">
<util:constant static-field="javax.xml.xpath.XPathConstants.STRING"/>
</property>
Spring can create a QName for you like this:
<bean id="qName" class="java.xml.namespace.QName">
<constructor index="0" value="localpart"/>
<constructor index="1" value="namespaceURI"/>
</bean>
Replace localpart and namespaceURI with the local name and namespace.
To reference a constant in a class, like javax.xml.xpath.XPathConstants.STRING
<bean id="qNameString" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="targetField" value="javax.xml.xpath.XPathConstants.STRING"/>
</bean>
A shorter version is available with the util schema:
<util:constant static-field="java.xml.xpath.XPathConstants.STRING"/>
Apart from being shorter, the id of the bean will be java.xml.xpath.XPathConstants.STRING rather than qNameString.
See FieldRetrievingFactoryBean and The util schema

Spring BeanNameUrlHandlerMapping maps pattern url

I have this particular query regarding BeanNameUrlHandlerMapping in Spring. Part of my dispatcher-servlet.xml looks like below -
<bean id="beanNameUrl" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/start.htm" class="controller.StartController"/>
<bean name="/login.htm" class="controller.LoginController"/>
<bean name="/company.htm" class="controller.CompanyController"/>
<bean name="/client.htm" class="controller.ClientController"/>
<bean name="/paginate.htm" class="controller.PaginateController"/>
<bean name="/detail_view.htm" class="controller.DetailViewController"/>
<bean name="/edit_view.htm" class="controller.EditViewController"/>
<bean name="/create_view.htm" class="controller.CreateViewController"/>
<bean name="/building.htm" class="controller.BuildingController"/>
<bean name="/tower.htm" class="controller.TowerController"/>
<bean name="/floor.htm" class="controller.FloorController"/>
<bean name="/space.htm" class="controller.SpaceController"/>
<bean name="/contract.htm" class="controller.ContractController"/>
<bean name="/space_package.htm" class="controller.SpacePackageController"/>
<bean name="/charge_head.htm" class="controller.ChargeHeadController"/>
<bean name="/search_view.htm" class="controller.SearchViewController"/>
Now, what happens is, whenever am trying to get to the space_package.htm, it always ends up in space.htm and to top it all, whatever I type in place of the * in http://host:port/app_name/space*.htm, it always end up in space.htm !!
Please advise how should I resolve this and where am I going worng.
Appreciate your help...:)
The URLs are named incorrectly. According to web standard, An URL can not contain special character (like "_"), so that /space_package.html is invalid. You may want to try /spacePackage.html instead.
For further understanding, you can refer the document in this link.

Resources