Sunday, August 2, 2015

Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource


Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]

I encountered this error when, i was doing a sample web application, using Annotations fully. Below is the sample snippet, which triggered the above issue. Once i registered the AppConfig to the AnnotationConfigWebApplicationContext, i added context.refresh, to fully process the class, which results in the above exception, when i removed the context.refresh, everything seems to be fine, but when i add it back, i am getting the same exception, so what triggers the issue?

public class AppInitializer implements WebApplicationInitializer{
 
 private static final String MAPPING_URL = "/";

 public void onStartup(ServletContext servletContext) throws ServletException {
  AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
  context.register(AppConfig.class); 
  servletContext.addListener(new ContextLoaderListener(context));
  context.refresh();
  ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new CustomDispatcherServlet(context));
  dispatcher.setLoadOnStartup(1);
  dispatcher.addMapping(MAPPING_URL);  
 } 
 
}

As per DispatcherServlet Constructor as specified here

  • If the given context does not already have a parent, the root application context will be set as the parent.
  • If the given context has not already been assigned an id, one will be assigned to it
  • ServletContext and ServletConfig objects will be delegated to the application context
  • FrameworkServlet.postProcessWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) will be called
  • Any ApplicationContextInitializers specified through the "contextInitializerClasses" init-param or through the
  • FrameworkServlet.setContextInitializers(org.springframework.context.ApplicationContextInitializer...) property will be applied.
  • refresh() will be called if the context implements ConfigurableApplicationContext

If the context has already been refreshed, none of the above will occur, under the assumption that the user has performed these actions (or not) per their specific needs.

So for the DispatcherServlet, you modify the code as below

AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
context.register(WebConfig .class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new CustomDispatcherServlet(webContext));

Sample Code of the WebConfig class as shown below.

@Configuration
@Configuration
@EnableWebMvc
@ComponentScan("com.practice")
public class WebConfig extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);

}

Happy Programming...!!!

Tuesday, July 7, 2015

Websphere application start error - A composition unit name already exists.

When you try to deploy an application or perform any administrative actions in Websphere, the following will be thrown:

A composition unit with name already exists. Select a different application name.

Cause
The issue is most frequently observed during the installation or removal of application, Say when you are doing a deployment, and there is an error in your deployment, then this bad deployment is saved in the server, this makes the Websphere go out of sync, since the previous deployment was already saved, when you are trying to do a fresh deployment, you will get above error saying that there is a unit already exist with your application name.
Even after you remove the application, you might not be able to do any further or new deployment.

Solution
The manual workaround is to delete the respective bad application folders from /blas and /cus directories.
The resource are located in the following config directory of the profile

/config/cells/cell/blas
/config/cells/cell/cus

For example if you see the error while installing MySampleApp.ear
Delete the following directory

/config/cells/cell/blas/MySampleApp
/config/cells/cell/cus/MySampleApp.

Once deleted, re-deploy your application.


Happy Programming...!!


Friday, October 10, 2014

MongoDB in Windows- error system cannot find the file specified

When you start MongoDB from the command prompt (not from the bin folder), you may get this error saying "error system cannot find the specified". This doesn't give you enough information on what triggers this error. Go to the respective bin folder of MongoDB and start the MongoDB, you will get the below error. The default db path for mongodb is /data/db/, MongoDB checks if /data/db/ is present and if the user has access to it. In your case, there is no such directory and hence the error.

mongod.exe --help for help and startup options
2014-10-10T17:45:58.896+0530 [initandlisten] MongoDB starting : pid=10100 port=27017 dbpath=\data\db\ 64-bit host=EEI3054
2014-10-10T17:45:58.897+0530 [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2014-10-10T17:45:58.897+0530 [initandlisten] db version v2.6.5
2014-10-10T17:45:58.897+0530 [initandlisten] git version: e99d4fcb4279c0279796f237aa92fe3b64560bf6
2014-10-10T17:45:58.897+0530 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2
Pack 1') BOOST_LIB_VERSION=1_49
2014-10-10T17:45:58.898+0530 [initandlisten] allocator: system
2014-10-10T17:45:58.898+0530 [initandlisten] options: {}
2014-10-10T17:45:58.903+0530 [initandlisten] exception in initAndListen: 10296
*********************************************************************
ERROR: dbpath (\data\db\) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating


However you can override the default db path using the --dbpath argument of mongod. Try running the below command. I have provided dbpath of my configuration, you can change it where you want to have it.

mongod.exe --dbpath "c://data/db"

In this case instead of checking for /data/db/ mongoDB check for c://data/db. In your case, you have the specified directory and you have access to it and hence it runs.

Happy Programming...!!!

Saturday, May 10, 2014

Hadoop File System (HDFS)

In this tutorial we will see, some of the common unix shell commands applied on the HDFS. To execute the commands on the HDFS (Hadoop Distributed File System), make sure that the Hadoop is running.

All unix shell commands, will executed against the default home directory in HDFS. What is the default home directory in HDFS? A user’s home directory in HDFS is located at /user/username. For example, my home directory is /user/mramanujam.

Lets start with some commands, make sure that hadoop is running, if not please start hadoop.

$start-dfs.sh
.....
$start-yarn.sh

Once started, let's create the HDFS home directory, it the same unix shell command to create a directory.

$ hadoop fs -mkdir -p /user/mramanujam

Now list the files in your home directory

$ hadoop fs -ls This is will list the files in our HDFS directory.

Lets create a file and move it to the HDFS

$ vi newFile.txt

Now copy the file create from the local directory to the HDFS directory

$ hadoop fs -copyFromLocal newFile.txt newFile.txt

Now list the files in your home directory now

$ hadoop fs -ls It will show Found 1 item and the relevant details.

To get the list of all commands, please visit here

Happy Programming...!!!


Saturday, April 19, 2014

java.lang.IllegalStateException: ArrayAdapter requires the resources ID to be a TextView

Recently i was creating ListActivity for my Android application. After doing the necessary work, when i try to run the application, i was spatted with the error java.lang.IllegalStateException: ArrayAdapter requires the resources ID to be a TextView. What causes this issue?.

When you are passing an layout to the constructor like this new ArrayAdapter(this, R.layout.list_employee, this.employeeList). You need to make sure that your layout file list_employee wasn't wrapped by another layout. Unfortunately, my layout was wrapped with the RelativeLayout , which results in the above exception.

Below is the layout of my activity, which results in the exception

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
  >  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/employee_list_heading" />  
 </RelativeLayout>  

To Resolve the issue, i have just removed the layout element which just wrapped my TextView, and modified my XML as below

 <?xml version="1.0" encoding="utf-8"?>  
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"   
   // other attributes of the TextView  
 />  

Happy Programming...!!!

Sunday, January 26, 2014

Hibernate mapping exception: Could not determine type for columns: [org.hibernate.mapping.Column]

Recently when i was implementing Many to Many with Join columns with Additional column, i was getting the following error when i was building my application. Below is the exception from my application stack.

Invocation of init method failed; nested exception is org.hibernate.MappingException: 
Could not determine type for: com.expense.domain.Medicine, at table: Medicine_Shop, for columns: [org.hibernate.mapping.Column(medicine)]

This is because my application doesn't find all of your annotations, because I was annotating both fields and methods. You have to use only one strategy.
Either annotate in fields else methods, here in my case, i was annotating at field level in all classes, except in one class, where i have annotated at the method level (getter/setter) because of that annotation doesn't have any effect. Below is the sample code, which triggers the above exception, where you can see in the MedicineShop class, i have Annotated @EmbeddedId at the method level instead of the field level, Once i move the annotation to the field level, the issue is resolved.

@Entity
@Table(name="Medicine")
public class Medicine{
 
 @Id
 @GeneratedValue
 private int id;
 @Column(name="Name")
 private String name;
 @Column(name="CompanyName")
 private String companyName;
 @OneToMany(fetch=FetchType.LAZY, mappedBy="pk.medicine", cascade=CascadeType.ALL)
 private Set medicineShop= new HashSet();
}

@Entity
@Table(name="Medicine_Shop")
@AssociationOverrides({
 @AssociationOverride(name="pk.medicine", 
   joinColumns= @JoinColumn(name = "medicine_id")),
 @AssociationOverride(name="pk.shop", 
   joinColumns= @JoinColumn(name = "shop_id"))
})
public class MedicineShop implements Serializable 
{
 
 private MedicineShopId pk = new MedicineShopId ();
 @Column(name="capacity", length=10, nullable=false)
 private int capacity;
 
 @EmbeddedId
 public MedicineShopId getPk() {
  return pk;
 }
 public void setPk(MedicineShopId pk) {
  this.pk = pk;
 }
}


Happy Programming...!!!

Saturday, October 19, 2013

Angular JS - Basic Introduction and Example

AngularJS is a javascript framework supported by Google that embraces extending HTML into a more expressive and readable format. It decreases emphasis on directly handling DOM manipulation from the application logic, allowing for easier testing. It employs efficient two-way data binding and sensible MVC implementation, reducing the server load of applications, a new wave of Single Page Applications (SPA). Your application is defined with modules that can depend from one to the others. It also encapsulates the behavior of your application in controllers which are instanciated thanks to dependency injection. Lets start with the well know example in the computure world, Hello World !!!, why can't we change a bit lets say Hello Universe.

Before we can do anything we need to create a simple HTML page in that we can include AngularJS. Create a file called index.html and use the following code: which is just a normal html code, which includes Angular JS Javascript, regular label, textbox and two new items.

 <!DOCTYPE html>  
 <html ng-app>  
 <head>  
   <title>Learning AngularJS - Hello Universe</title>  
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>  
 </head>  
 <body>     
   Write Your Name in Textbox:  
   <input type="text" ng-model="yourname" />  
   <h1>Hello Universe by : {{ yourname }}</h1>  
 </body>  
 </html>  

First item which strikes our eyes is ng-app, which tells the active portion of he page. The directive ng-app will cause Angular to auto initialize your application.
Nextone is the attribute ng-model in textbox as ng-model="yourname". Whenever Angular sees this directive ng-model it automatically sets up two-way data binding.
How this works? When this page is loaded, Angualar bounds the state of text with model, thus when the user changes the value, model yourname will get's
automatically changed.
And finally, the mysterious expression {{ yourname }}: Which tells Angular to bind the value of model yourname in place of {{ yourname }}.

That's it we have just learned how to say Hello Universe through Angular

Happy Programming ...!!!