Wednesday, 19 August 2015

Error In Starting of Mule App

Some times Mule app failed to start , throwing weird exception like this :
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'mule'.
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
        at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
        at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(
        at org.mule.config.spring.MuleApplicationContext.loadBeanDefinitions(
        at org.mule.config.spring.SpringRegistry.doInitialise(
        at org.mule.registry.AbstractRegistry.initialise(
        at org.mule.config.spring.SpringXmlConfigurationBuilder.createSpringRegistry(
        at org.mule.config.spring.SpringXmlConfigurationBuilder.doConfigure(
        at org.mule.context.DefaultMuleContextFactory.createMuleContext(
        at org.mule.module.launcher.application.DefaultMuleApplication.init(
        at org.mule.module.launcher.application.ApplicationWrapper.init(
        at org.mule.module.launcher.DefaultMuleDeployer.deploy(
        at org.mule.module.launcher.MuleDeploymentService.guardedDeploy(
        at org.mule.module.launcher.MuleDeploymentService.start(
        at org.mule.module.launcher.MuleContainer.start(
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.mule.module.reboot.MuleContainerWrapper.start(
        at org.tanukisoftware.wrapper.WrapperManager$
[28/01/2015 18:17:07 INFO  DefaultMuleApplication:197] App 'plum-slice-rest-api-3.3.4-SNAPSHOT' never started, nothing to dispose of
ERROR 2015-01-28 18:17:07,426 [WrapperListener_start_runner] org.mule.module.launcher.MuleDeploymentService:
+ Failed to deploy app                                     +
+ 'test', see below          +
The solution which you can try in this case pretty simple .
Just delete .mule folder
Post Comments and Suggestions !!

Thursday, 6 August 2015

Double Colon Operator in Java

Java 8 has introduced double colon (::) apart from lambda expressions. Let's see how does it work .

public class DoubleColonTest {
  interface MyInterface{
      public void interfaceMethod(String input);
  public static void staticmethod(String input)
    System.out.println(" static mymethod  " + input);
  public void method1(String input)
    System.out.println(" mymethod  " + input);
  public static void main(String[] args) {
    DoubleColonTest obj = new DoubleColonTest();
    MyInterface dd = obj::method1;
    dd.interfaceMethod(" test string ");
    MyInterface second = DoubleColonTest::staticmethod;
    second.interfaceMethod(" test static string ");

In this example , we have a functional interface with method name interfaceMethod , and we have a DoubleColonTest class which has two method which takes same parameters as interfaceMethod .Now this line :
MyInterface dd = obj::method1;
This line tells the compiler to create an implementation of MyInterface by applying the body of method1 defined in the DoubleColonTest class . Since this method is instance method , we need to use instance of class. This is called instance method reference .We can take static method reference also like this :
MyInterface second = DoubleColonTest::staticmethod;
So , now we can reference methods and reuse them again and again . We can also reference Constructor also , but we need to have a constructor which matches the interface method signature like this :
public DoubleColonTest(String in)
    System.out.println(" constructor  " + in);
Now to create an implementation of MyInterface , just need to write :
 MyInterface third = DoubleColonTest::new;
 third.interfaceMethod(" test string ");   
please post comments and suggestions !!!

Tuesday, 4 August 2015

How Does Lambdas Work In Java

In this post , i will show how lambdas in java work , with a simple example .Consider the following code snipped using java lambda .
package java8Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LambdaTest2 {
  public static void main(String[] args) {
      ExecutorService service = Executors.newSingleThreadExecutor();
      service.submit(()-> {
        File f = new File("abcd.txt");
        FileReader reader = new FileReader(f);
        //use reader to read stuff
        return null ;
The above code compiles fine , now if you remove return statement(which seems redundant here) from above code , it fails to compile with following error :
Unhandled exception type FileNotFoundException
Reason for this behavior is , ExecutorService's submit method has two overloaded versions . One takes Callable , and the other one takes Runnable , Both methods have no arguments .

So , which one the java compiler chooses ?

It looks for return type as well . When you write return statement , it creates Callable and when you return nothing , it creates Runnable . Since Runnable does not throw Checked Exceptions , that is why you get compile time errors in the code .

You can explicitly cast it to Callable by using cast operator like this :
service.submit((Callable)()-> {
You can add marker interfaces also in the cast like this :
Callable calls = (Callable & Serializable) () -> { return null; };
Lambda Removes a lot of boilerplate code , but it might behave differently if you don't know how it works .
You can go to original link here

Thursday, 23 July 2015

Top Underrated Git Commands

Here are the most important commands in git , and their description .

git reset –hard origin/master 
Resets your Local Repo with HEAD
git reflog
Displays a walkthrough of git cycle through out your local repo with commit references.
git cherry
Note: Cherry and Cherry-pick are two different commands, Cherry displays a list of commit differences between branches/remotes
e.g git cherry master staging OR git cherry -v staging
git cherry-pick
Adds a commit to your current branch.
git shortlog
Displays Author and no of commits.e.g git shortlog -s -n
git shortlog
Everyone of us must be knowing what git logs can do , Today lets trick it out a advanced feature of Git log.
Lets take an example , Some one have merged master with staging and pushed accidentally.
I want to know who did it , who merged the changes because git will say the commit author name.
To do so we need to run
git log --pretty='%T %s --Commiter: %cn --Author: %aN'
Other method would be to open gitg and check author and committer name.
Author name is who changed the content in file and commiter is who merged or changed the commit history to your branch.

Thursday, 25 June 2015

Solved : Mule With Maven Failing

Recently , i encountered an error with maven build.

The pom file which i was using , started failing with failed dependency errors .It was working fine earlier.
Here is the error which i was getting while trying to build mule app with maven .

 Failed to execute goal on project middleware-services: Could not resolve dependencies for project com.javaroots:my-services:mule:1.0: Failed to collect dependencies at org.mule.transports:mule-transport-vm:jar:3.5.0 -> org.mule.modules:mule-module-xml:jar:3.5.0 -> Failed to read artifact descriptor for Could not transfer artifact from/to codehaus ( Failed to transfer file: Return code is: 410, ReasonPhrase: Gone. -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project my-services: Could not resolve dependencies for project com.javaroots:middleware-services:mule:1.0: Failed to collect dependencies at org.mule.transports:mule-transport-vm:jar:3.5.0 -> org.mule.modules:mule-module-xml:jar:3.5.0 ->
 at org.apache.maven.lifecycle.internal.LifecycleDependencyResolver.getDependencies(
 at org.apache.maven.lifecycle.internal.LifecycleDependencyResolver.resolveProjectDependencies(
 at org.apache.maven.lifecycle.internal.MojoExecutor.ensureDependenciesAreResolved(
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(
 at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(
 at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(
 at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(
 at org.apache.maven.DefaultMaven.doExecute(
 at org.apache.maven.DefaultMaven.doExecute(
 at org.apache.maven.DefaultMaven.execute(
 at org.apache.maven.cli.MavenCli.execute(
 at org.apache.maven.cli.MavenCli.doMain(
 at org.apache.maven.cli.MavenCli.main(
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(
 at java.lang.reflect.Method.invoke(
 at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(
 at org.codehaus.plexus.classworlds.launcher.Launcher.launch(
 at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(
 at org.codehaus.plexus.classworlds.launcher.Launcher.main(

Caused by: org.eclipse.aether.resolution.ArtifactResolutionException: Could not transfer artifact from/to codehaus ( Failed to transfer file: Return code is: 410, ReasonPhrase: Gone.
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(
 at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(
 ... 41 more
Caused by: org.eclipse.aether.transfer.ArtifactTransferException: Could not transfer artifact from/to codehaus ( Failed to transfer file: Return code is: 410, ReasonPhrase: Gone.
 at org.eclipse.aether.connector.basic.ArtifactTransportListener.transferFailed(
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector$
 at org.eclipse.aether.util.concurrency.RunnableErrorForwarder$
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector$DirectExecutor.execute(
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector.get(
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(
 ... 44 more
Caused by: org.apache.maven.wagon.TransferFailedException: Failed to transfer file: Return code is: 410, ReasonPhrase: Gone.
 at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.resourceExists(
 at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.resourceExists(
 at org.eclipse.aether.transport.wagon.WagonTransporter$
 at org.eclipse.aether.transport.wagon.WagonTransporter.execute(
 at org.eclipse.aether.transport.wagon.WagonTransporter.peek(
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector$PeekTaskRunner.runTask(
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector$
 ... 49 more
The reason for failure of this is , Codehaus no longer serves up Maven repositories . So we need to change maven settings a little bit to make this work . Go to your M2_HOME/conf folder and open settings.xml .

Create following repo profile and activate it .
And run the project again . It will work .

Ref : StackOverflow

Post Comments And Suggestions !!

Tuesday, 23 June 2015

How to Commit only part of a file in Git

Today we will be discussing How to Commit only a Part of File in Git , Instead of Commiting the whole file , basically useful if you have dev ,prod ,and other environment parameters in same file

Lets Take an Example of a file named database.yml which has three environment(development, staging and production).

I have changed the Credentials for Development/staging/Production, and I need to commit the file for production/staging change frequently keeping development changes in my local.

So, Instead of reverting the changes of development and commiting the files we can Patch the changes into chunks and commit.

Syntax : git add --patch filename.x (or -p for short) Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?

And here the meaning of each option:

y stage this hunk for the next commit.
n do not stage this hunk the next commit
q quit; do not stage this hunk or any of the remaining ones
a stage this hunk and all later hunks in the file
d do not stage this hunk or any of the later hunks in the file
g select a hunk to go to
/ search for a hunk matching the given regex
j leave this hunk undecided, see next undecided hunk
J leave this hunk undecided, see next hunk
k leave this hunk undecided, see previous undecided hunk
K leave this hunk undecided, see previous hunk
s split the current hunk into smaller hunks
e manually edit the current hunk
? print help

In Our Example:

git add --patch config/database.yml
So It will break the changes into chunk and ask :


   adapter: oracle_enhanced

-  database: //localhost:29801/mydb

-  username: mydb

-  password: mydb

+  database: //

+  username: santosh

+  password: santosh 

Commit only part of a file in Git 

Stage this hunk [y,n,q,a,d,/,K,g,s,e,?]? y
Continue Providing the Options for Development & Production .

Finally you will see a Staged Config/database.yml and a unstaged config/database.yml

So this is how we commit part file in git .

Thanks To Santosh for writing this post .

Friday, 19 June 2015

How To Enable Mass Storage on YU

This document describes how to connect mobile phones (like Yuphoria,Yureka), which are based on Cyanogenmod OS as Mass Storage/MTP in Linux.

This is a solution for ticket : post will enable you to mount your device sd card and internal storage on PC .


Firstly we're going to need to install some of the common MTP apps that will be needed. Open up a terminal and type the following two lines one after the other.
sudo apt-get install libmtp-common mtp-tools libmtp-dev libmtp-runtime libmtp9
sudo apt-get dist-upgrade

Then we're going to amend the fuse.conf file. At the terminal type
sudo nano /etc/fuse.conf
We want to remove the # from the below line of code for user_allow_other, like so...
#/etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE)

#Set the maximum number of FUSE mounts allowed to non-root users.
#The default is 1000.
#mount_max = 1000

# Allow non-root users to specify the allow_other or allow_root mount options.

Now save the file by pressing Ctrl+x, press Y and then Enter.


We now need to set up some rules for our device that we plan on connecting, but before we do that we need to find out.

Connect your device via an available usb port and from terminal enter
This should bring up an output similar to the following

usermac44@usermac44:~/workspace/ps$ lsusb 
Bus 002 Device 004: ID 17ef:6019 Lenovo  
Bus 002 Device 003: ID 04b3:3025 IBM Corp. NetVista Full Width Keyboard 
Bus 002 Device 003: ID 0fce:01b1 Yuphoria Inc.  
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub 
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 

You need to look for your device, in this instance my Yuphoria in the list, the vendor id is 0fce and product id is 01b1 .


We're then going to amend the mtp udev rules as follows, from a terminal type :
sudo nano /lib/udev/rules.d/69-mtp.rules
# Yuphoria YU5010
ATTR{idVendor}=="0fce", ATTR{idProduct}=="01b1", SYMLINK+="libmtp-%k", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"
Once done, save the file.


The next step would also be to add a line of code to the 51 android rules file, again from a terminal type
sudo nano /etc/udev/rules.d/51-android.rules
ATTR{idVendor}=="0fce", ATTR{idProduct}=="01b1", MODE=”0666"
put your device product id and vendor id here .

Once that is done and the file is saved, remove any usb device currently connected and issue the following commands to reboot the system.
sudo service udev restart
sudo reboot
Enjoy Using Mass Storage in Yureka/Yuphoria (Cyanogen) Mobile Phones.

Post Comment , if any better solutions found