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(DefaultDocumentLoader.java:75)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
        at org.mule.config.spring.MuleApplicationContext.loadBeanDefinitions(MuleApplicationContext.java:113)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
        at org.mule.config.spring.SpringRegistry.doInitialise(SpringRegistry.java:89)
        at org.mule.registry.AbstractRegistry.initialise(AbstractRegistry.java:109)
        at org.mule.config.spring.SpringXmlConfigurationBuilder.createSpringRegistry(SpringXmlConfigurationBuilder.java:119)
        at org.mule.config.spring.SpringXmlConfigurationBuilder.doConfigure(SpringXmlConfigurationBuilder.java:73)
        at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:46)
        at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
        at org.mule.config.builders.AutoConfigurationBuilder.autoConfigure(AutoConfigurationBuilder.java:101)
        at org.mule.config.builders.AutoConfigurationBuilder.doConfigure(AutoConfigurationBuilder.java:57)
        at org.mule.config.builders.AbstractConfigurationBuilder.configure(AbstractConfigurationBuilder.java:46)
        at org.mule.config.builders.AbstractResourceConfigurationBuilder.configure(AbstractResourceConfigurationBuilder.java:78)
        at org.mule.context.DefaultMuleContextFactory.createMuleContext(DefaultMuleContextFactory.java:84)
        at org.mule.module.launcher.application.DefaultMuleApplication.init(DefaultMuleApplication.java:207)
        at org.mule.module.launcher.application.ApplicationWrapper.init(ApplicationWrapper.java:64)
        at org.mule.module.launcher.DefaultMuleDeployer.deploy(DefaultMuleDeployer.java:47)
        at org.mule.module.launcher.MuleDeploymentService.guardedDeploy(MuleDeploymentService.java:420)
        at org.mule.module.launcher.MuleDeploymentService.start(MuleDeploymentService.java:186)
        at org.mule.module.launcher.MuleContainer.start(MuleContainer.java:160)
        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(MuleContainerWrapper.java:56)
        at org.tanukisoftware.wrapper.WrapperManager$12.run(WrapperManager.java:2788)
[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.io.File;
import java.io.FileReader;
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 -> net.java.dev.stax-utils:stax-utils:jar:20080702: Failed to read artifact descriptor for net.java.dev.stax-utils:stax-utils:jar:20080702: Could not transfer artifact net.java.dev.stax-utils:stax-utils:pom:20080702 from/to codehaus (http://repository.codehaus.org): Failed to transfer file: http://repository.codehaus.org/net/java/dev/stax-utils/stax-utils/20080702/stax-utils-20080702.pom. 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 -> net.java.dev.stax-utils:stax-utils:jar:20080702
 at org.apache.maven.lifecycle.internal.LifecycleDependencyResolver.getDependencies(LifecycleDependencyResolver.java:221)
 at org.apache.maven.lifecycle.internal.LifecycleDependencyResolver.resolveProjectDependencies(LifecycleDependencyResolver.java:127)
 at org.apache.maven.lifecycle.internal.MojoExecutor.ensureDependenciesAreResolved(MojoExecutor.java:257)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:200)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
 at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
 at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
 at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
 at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
 at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
 at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
 at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
 at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
 at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
 at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:483)
 at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
 at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
 at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
 at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

Caused by: org.eclipse.aether.resolution.ArtifactResolutionException: Could not transfer artifact net.java.dev.stax-utils:stax-utils:pom:20080702 from/to codehaus (http://repository.codehaus.org): Failed to transfer file: http://repository.codehaus.org/net/java/dev/stax-utils/stax-utils/20080702/stax-utils-20080702.pom. Return code is: 410, ReasonPhrase: Gone.
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:444)
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:246)
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:223)
 at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:287)
 ... 41 more
Caused by: org.eclipse.aether.transfer.ArtifactTransferException: Could not transfer artifact net.java.dev.stax-utils:stax-utils:pom:20080702 from/to codehaus (http://repository.codehaus.org): Failed to transfer file: http://repository.codehaus.org/net/java/dev/stax-utils/stax-utils/20080702/stax-utils-20080702.pom. Return code is: 410, ReasonPhrase: Gone.
 at org.eclipse.aether.connector.basic.ArtifactTransportListener.transferFailed(ArtifactTransportListener.java:43)
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:355)
 at org.eclipse.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:67)
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector$DirectExecutor.execute(BasicRepositoryConnector.java:581)
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector.get(BasicRepositoryConnector.java:249)
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:520)
 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:421)
 ... 44 more
Caused by: org.apache.maven.wagon.TransferFailedException: Failed to transfer file: http://repository.codehaus.org/net/java/dev/stax-utils/stax-utils/20080702/stax-utils-20080702.pom. Return code is: 410, ReasonPhrase: Gone.
 at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.resourceExists(AbstractHttpClientWagon.java:724)
 at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.resourceExists(AbstractHttpClientWagon.java:680)
 at org.eclipse.aether.transport.wagon.WagonTransporter$PeekTaskRunner.run(WagonTransporter.java:518)
 at org.eclipse.aether.transport.wagon.WagonTransporter.execute(WagonTransporter.java:427)
 at org.eclipse.aether.transport.wagon.WagonTransporter.peek(WagonTransporter.java:398)
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector$PeekTaskRunner.runTask(BasicRepositoryConnector.java:376)
 at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:350)
 ... 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 .
<profile>
   <id>mule</id>
  <repositories>
  <repository>
    <id>codehaus-mule-repo</id>
    <name>codehaus-mule-repo</name>
    <url>
      https://repository-master.mulesoft.org/nexus/content/groups/public/
    </url>
    <layout>default</layout>
  </repository>
</repositories>
 </profile>
<activeProfiles>
    <activeProfile>mule</activeProfile>
</activeProfiles>
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 :

 production:

   adapter: oracle_enhanced

-  database: //localhost:29801/mydb

-  username: mydb

-  password: mydb

+  database: //192.168.1.45:1521/ORCL

+  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 : https://jira.cyanogenmod.org/browse/YU-4This post will enable you to mount your device sd card and internal storage on PC .

STEP 1

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
STEP 2

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.
user_allow_other

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


STEP 3

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
lsusb
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 .


STEP 4

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.


STEP 5

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