Just to track my reading list. Here are what I have finished reading recently:
Both books has been written by StackOverflow. He is great at writing and has spacious mind. Just read the TOC and see if you want to read that books.
This blog is a collection of minds around linux, java, javascript, etc. Looking for great opportunities.
вторник, 18 августа 2015 г.
понедельник, 10 августа 2015 г.
GWT 2.7.0. Deserialize json on server with Autobean framework
One can use JSON libraries to deserialize json to POJO. How to do it with GWT?
GWT ships with Autobean framework. It allows you to serialize the json on client. But, we can use it on the server side too.
Here is the magic:
1) define the the interface which extends the AutoBeanFactory
2) on the server side use beanFactory = AutoBeanFactorySource.create(Your_Factory_class.class);
AutoBeanFactorySource is in com.google.web.bindery.autobean.vm.AutoBeanFactorySource package. One clue is that it is experimental. So check how it is working in your environment.
3) To decode json to POJO use:
AutoBean<Your_POJOInterface> bean = AutoBeanCodex.decode(
beanFactory, Your_POJOInterface.class, jsonString);
Your_POJOInterface requestObject = bean.as();
4) That is all! Just plain GWT and no other special mappers!
GWT ships with Autobean framework. It allows you to serialize the json on client. But, we can use it on the server side too.
Here is the magic:
1) define the the interface which extends the AutoBeanFactory
2) on the server side use beanFactory = AutoBeanFactorySource.create(Your_Factory_class.class);
AutoBeanFactorySource is in com.google.web.bindery.autobean.vm.AutoBeanFactorySource package. One clue is that it is experimental. So check how it is working in your environment.
3) To decode json to POJO use:
AutoBean<Your_POJOInterface> bean = AutoBeanCodex.decode(
beanFactory, Your_POJOInterface.class, jsonString);
Your_POJOInterface requestObject = bean.as();
4) That is all! Just plain GWT and no other special mappers!
четверг, 6 августа 2015 г.
Check GWT and SmartGWT versions in project
You have a GWT & SmartGWT project. How to check the version of this jar libs in project?
For the GWT version open the *.cache.* file and see the *$gwt_version* variable.
As for SmartGWT see the class com.smartgwt.client.Version class.
For the GWT version open the *.cache.* file and see the *$gwt_version* variable.
As for SmartGWT see the class com.smartgwt.client.Version class.
пятница, 31 июля 2015 г.
J2EE. Logging in application
Logging is an cross-cutting concern in the application.
It is tedious to write every time
To speed up one can copy this string from one class to another.
Not very expressive way. Well, it is more code to read.
Nowadays we have annotations, we have EJB, we have CDI.
We can improve the logging code in two ways:
It is tedious to write every time
Logger log = LogManager.getLogger(getClass());
To speed up one can copy this string from one class to another.
Not very expressive way. Well, it is more code to read.
Nowadays we have annotations, we have EJB, we have CDI.
We can improve the logging code in two ways:
- implement the logging as a cross-cutting concern with a CDI interceptor
- if we need more logging it would be much simpler to inject logger
четверг, 30 июля 2015 г.
Get info about user in WebSphere Application Server 8.5.5
Sometimes you should get info about user in WebSphere Application Server. WAS is connected to ldap. I have to find out the full name of a user. How to do it?
One can use the VMM - the subsystem for user management in WAS.
To compile the VMM code following jars have to be in the classpath:
<WAS_HOME>\plugins\com.ibm.ws.runtime.jar
<WAS_HOME>\plugins\com.ibm.ws.runtime.wim.base.jar
<WAS_HOME>\plugins\org.eclipse.emf.commonj.sdo.jar
<WAS_HOME>\lib\j2ee.jar
Here is the code to get the user name:
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.ibm.websphere.wim.SchemaConstants;
import com.ibm.websphere.wim.Service;
import com.ibm.websphere.wim.client.LocalServiceProvider;
import com.ibm.websphere.wim.ras.WIMTraceHelper;
import com.ibm.websphere.wim.util.SDOHelper;
import commonj.sdo.DataObject;
class VMMRealm {
private static Logger log = LogManager.getLogger(VMMRealm.class);
// Virtual member manager service that is used to make API calls
static Service service = null;
static {
service = locateService();
}
@SuppressWarnings("unchecked")
public UserInfo getUserData(String login) throws UserStoreAccessException {
UserInfo userInfo = null;
DataObject root = null;
try {
root = SDOHelper.createRootDataObject();
DataObject searchCtrl = SDOHelper.createControlDataObject(root,
null, SchemaConstants.DO_SEARCH_CONTROL);
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add(
"telephoneNumber");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add(
"createTimestamp");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES)
.add("givenName");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("title");
searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, String
.format("@xsi:type='PersonAccount' and uid='%s'", login));
log.trace(printDO(root));
root = service.search(root);
log.trace(printDO(root));
userInfo = new UserInfo();
convertDataObjectToUserInfo(root, userInfo);
} catch (Exception e) {
throw new UserStoreAccessException("Error getting user", e);
}
return userInfo;
}
/**
* Convert data object to user info
*
* @param root
* @param info
*/
private void convertDataObjectToUserInfo(DataObject root, UserInfo info) {
List entities = root.getList(SchemaConstants.DO_ENTITIES);
for (int i = 0; i < entities.size(); i++) {
DataObject ent = (DataObject) entities.get(i);
info.setCn(ent.getString("cn"));
info.setUid(ent.getString("uid"));
}
}
/**
* Loop through the entities in the DataObject and print its uniqueName
*
* @param root
* input DataObject
*/
public static void printIdentifiers(DataObject root) throws Exception {
// Get all entities in the DataObject
List entities = root.getList(SchemaConstants.DO_ENTITIES);
for (int i = 0; i < entities.size(); i++) {
DataObject ent = (DataObject) entities.get(i);
// Get the entity Identifier
DataObject id = ent.getDataObject(SchemaConstants.DO_IDENTIFIER);
if (id != null) {
String uniqueName = id
.getString(SchemaConstants.PROP_UNIQUE_NAME);
log.debug("UniqueName is -> " + uniqueName);
} else {
log.debug("Missing Identifier");
}
}
}
/**
* Locates virtual member manager service in local JVM
**/
private static Service locateService() {
try {
// Local access virtual member manager Service
return new LocalServiceProvider(null);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
public static String printDO(DataObject obj) {
return WIMTraceHelper.printDataObject(obj);
}
}
To get info about the user call getUserData method.
One can use the VMM - the subsystem for user management in WAS.
To compile the VMM code following jars have to be in the classpath:
<WAS_HOME>\plugins\com.ibm.ws.runtime.jar
<WAS_HOME>\plugins\com.ibm.ws.runtime.wim.base.jar
<WAS_HOME>\plugins\org.eclipse.emf.commonj.sdo.jar
<WAS_HOME>\lib\j2ee.jar
Here is the code to get the user name:
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.ibm.websphere.wim.SchemaConstants;
import com.ibm.websphere.wim.Service;
import com.ibm.websphere.wim.client.LocalServiceProvider;
import com.ibm.websphere.wim.ras.WIMTraceHelper;
import com.ibm.websphere.wim.util.SDOHelper;
import commonj.sdo.DataObject;
class VMMRealm {
private static Logger log = LogManager.getLogger(VMMRealm.class);
// Virtual member manager service that is used to make API calls
static Service service = null;
static {
service = locateService();
}
@SuppressWarnings("unchecked")
public UserInfo getUserData(String login) throws UserStoreAccessException {
UserInfo userInfo = null;
DataObject root = null;
try {
root = SDOHelper.createRootDataObject();
DataObject searchCtrl = SDOHelper.createControlDataObject(root,
null, SchemaConstants.DO_SEARCH_CONTROL);
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add(
"telephoneNumber");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add(
"createTimestamp");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES)
.add("givenName");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("title");
searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, String
.format("@xsi:type='PersonAccount' and uid='%s'", login));
log.trace(printDO(root));
root = service.search(root);
log.trace(printDO(root));
userInfo = new UserInfo();
convertDataObjectToUserInfo(root, userInfo);
} catch (Exception e) {
throw new UserStoreAccessException("Error getting user", e);
}
return userInfo;
}
/**
* Convert data object to user info
*
* @param root
* @param info
*/
private void convertDataObjectToUserInfo(DataObject root, UserInfo info) {
List entities = root.getList(SchemaConstants.DO_ENTITIES);
for (int i = 0; i < entities.size(); i++) {
DataObject ent = (DataObject) entities.get(i);
info.setCn(ent.getString("cn"));
info.setUid(ent.getString("uid"));
}
}
/**
* Loop through the entities in the DataObject and print its uniqueName
*
* @param root
* input DataObject
*/
public static void printIdentifiers(DataObject root) throws Exception {
// Get all entities in the DataObject
List entities = root.getList(SchemaConstants.DO_ENTITIES);
for (int i = 0; i < entities.size(); i++) {
DataObject ent = (DataObject) entities.get(i);
// Get the entity Identifier
DataObject id = ent.getDataObject(SchemaConstants.DO_IDENTIFIER);
if (id != null) {
String uniqueName = id
.getString(SchemaConstants.PROP_UNIQUE_NAME);
log.debug("UniqueName is -> " + uniqueName);
} else {
log.debug("Missing Identifier");
}
}
}
/**
* Locates virtual member manager service in local JVM
**/
private static Service locateService() {
try {
// Local access virtual member manager Service
return new LocalServiceProvider(null);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
public static String printDO(DataObject obj) {
return WIMTraceHelper.printDataObject(obj);
}
}
To get info about the user call getUserData method.
J2EE6. CDI qualifier convenient injection
When you have an interface reference in a class and this interface has more than 1 implementation you have to specify somehow the injected implementation.
CDI offers you the qualifiers mechanism (annotations) to resolve this issue. For example you can create one annotation for PROD version and another annotation for TEST version of the implementation. Two annotations for such a thing are too much.
We can simplify it.
Here is our qualifier:
@Qualifier
@Retention(RUNTIME)
@Target({ FIELD, TYPE, METHOD })
public @interface ConnectionFactory {
ConnectionFactoryType value();
}
Here is the example of ConnectionFactoryType:
public enum ConnectionFactoryType {
PROD, TEST
}
Here is how how the implementation of the CDI bean looks like:
@ConnectionFactory(ConnectionFactoryType.PROD)
@ApplicationScoped
public class ProdConnectionFactory implements IConnectionFactory {}
Here is how you inject the bean:
@Inject
@ConnectionFactory(ConnectionFactoryType.PROD)
private IConnectionFactory connectionFactory;
What we got here is:
1) we created one annotation for different connection implementations.
2) We can specify the implementation by specifying the enum value.
3) This is reusable and better then creating one annotation per implementation.
CDI offers you the qualifiers mechanism (annotations) to resolve this issue. For example you can create one annotation for PROD version and another annotation for TEST version of the implementation. Two annotations for such a thing are too much.
We can simplify it.
Here is our qualifier:
@Qualifier
@Retention(RUNTIME)
@Target({ FIELD, TYPE, METHOD })
public @interface ConnectionFactory {
ConnectionFactoryType value();
}
Here is the example of ConnectionFactoryType:
public enum ConnectionFactoryType {
PROD, TEST
}
Here is how how the implementation of the CDI bean looks like:
@ConnectionFactory(ConnectionFactoryType.PROD)
@ApplicationScoped
public class ProdConnectionFactory implements IConnectionFactory {}
Here is how you inject the bean:
@Inject
@ConnectionFactory(ConnectionFactoryType.PROD)
private IConnectionFactory connectionFactory;
What we got here is:
1) we created one annotation for different connection implementations.
2) We can specify the implementation by specifying the enum value.
3) This is reusable and better then creating one annotation per implementation.
понедельник, 27 июля 2015 г.
Scala packages implicit import
By default in Scala following packages are implicitly imported in every unit of compilation:
- java.lang._
- scala._
- scala.Predef._
четверг, 23 июля 2015 г.
Singleton pattern in java
Currently I'm interested in software patterns. I'm reading the classic book: "Design patterns. Elements of reusable object-oriented software".
One of the patterns described is singleton. This is a popular pattern in java. So, how could you develop it java?
class Singleton{
private static Singleton singleton;
private Singleton{}
public static synchronized Singleton getInstance() {
if (instance == null) instance = new Singleton();
return instance;
}
}
But, how about this?
public enum Singleton{
INSTANCE;
public void yourBusinessMethod(){}
}
A single-element enum type is the best way to implement a singleton. This is stated in "Effective Java".
What are the pros of this solution?
One of the patterns described is singleton. This is a popular pattern in java. So, how could you develop it java?
class Singleton{
private static Singleton singleton;
private Singleton{}
public static synchronized Singleton getInstance() {
if (instance == null) instance = new Singleton();
return instance;
}
}
But, how about this?
public enum Singleton{
INSTANCE;
public void yourBusinessMethod(){}
}
A single-element enum type is the best way to implement a singleton. This is stated in "Effective Java".
What are the pros of this solution?
- no serialization problems
- guarantee against multiple instantiation
- easy to read (less) code
пятница, 19 июня 2015 г.
Productivity works
Do you like to work on multiple tasks simultaneously? If you are a computer, than you can. But if you are a human, that is hardly possible without sacrificing the quality of your job.
Can you do more working on multiple tasks simultaneously?
How much time do you spend on context switching between tasks?
Do you like when phone call, colleague or IM interrupts you during you work activity?
As for me I like to work on jobs in serial one by one. This means I can dedicate all my attention to the job. If you do multiple things at a time, than you can't complete them all.
Whenever possible, avoid interruptions and avoid working on more than one task at the same time. More on this one can read in the post by Kathy Sierra Your brain on multitasking.
Can you do more working on multiple tasks simultaneously?
How much time do you spend on context switching between tasks?
Do you like when phone call, colleague or IM interrupts you during you work activity?
As for me I like to work on jobs in serial one by one. This means I can dedicate all my attention to the job. If you do multiple things at a time, than you can't complete them all.
Whenever possible, avoid interruptions and avoid working on more than one task at the same time. More on this one can read in the post by Kathy Sierra Your brain on multitasking.
четверг, 18 июня 2015 г.
WebSphere Portal 7 webdav theme resources
First, to upload theme static resources to portal use webdav url:
http://<host>:10039/wps/mycontenthandler/dav/themelist.
Second, to make modifications in uploaded theme static resources use the following url:
http://<host>:10039/wps/mycontenthandler/dav/fs-type1.
Maybe, this will save up some time.
http://<host>:10039/wps/mycontenthandler/dav/themelist.
Second, to make modifications in uploaded theme static resources use the following url:
http://<host>:10039/wps/mycontenthandler/dav/fs-type1.
Maybe, this will save up some time.
Подписаться на:
Сообщения (Atom)