Wednesday, April 15, 2015

What is this about? raddiman

Monday, February 17, 2014

No Runnable Methods while running junit test cases

This happens when you have more than one class in unit test case suite. JUnit tries to run all the classes in the suite. But it may happen that some of the classes are already used by main class.
Example:
public class MainTestClass {
HelperClass hClass = new HelperClass(..);
hClass.doStuff();
}

public class HelperClass {
public HelperClass(..) {..}
doStuff(..) {..}
}

In this case, JUnit would try to run both of these classes, and then after running first class, it would throw exception for second class saying 'No Runnable methods' or sometimes it even says - Test class should have exactly one public zero argument constructor.

To solve this, annotate the second class with @Ignore so that JUnit does not try to run it separately. It also makes sense to do so because, second helper class is already being used by first class MainTestClass.
so finally:
import org.junit.Ignore
@Ignore
public class HelperClass {
.....
}

should fix the issue.

Saturday, October 6, 2012

Eclipse PDE - While creating XWT component and adding to Palette, End user sees the component twice when dragged and dropped

You create an XWT composite component, say button and name it as 'MyButton', then the wizard creates 2 files for you. One is MyButton.xwt and other one MyButton.java.
You will add the new component into palette by normal 'add component' wizard and it is ready for end user to be dragged and dropped.

But when an user drags and drops the 'MyButton' component from palette, it appears twice on UI.

Reason: Actually the two fies '.xwt' and '.java', both files would have the initialization routine by default. so when 'MyButton' component was dropped, both of them initialized and showed twice. Ideally '.java' file should be used as a handler to handle the events from UI. So To solve it, delete the initialization routine written inside the constructor of 'MyButton.java' file. That should fix this problem.
Another reason that might contribute to the double appearance is name of a property matching to parent class. Suppose inside MyButton.java file, you declare a property called 'size' and write getter/setter for this. Purpose of such property is to customize 'size' property and show 'size' property whenever user clicks on 'MyButton' component. BUT originally there is already a 'size' property in parent class...so when you write 'size' and build your component, It will show up twice when user drops the component from palette. You can rename your 'size' property to any other string...And that should fix this problem too. 

Tuesday, January 10, 2012

Maven build error Error transferring file: Permission denied: connect


Problem: When you try to do a maven build, then it tries to download all POM files or JAR files from maven repo, and it throws following error:
Error transferring file: Permission denied: connect

It is not able to download even 1 file from maven repo.

This is the complete stack trace when run with -X option:


E:\LearningProjects>mvn -X archetype:generate  -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -Dartifa
ctId=my-app
+ Error stacktraces are turned on.
Apache Maven 2.2.1 (r801777; 2009-08-07 00:46:01+0530)
Java version: 1.6.0_27
Java home: C:\Program Files\Java\jdk1.6.0_27\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7" version: "6.1" arch: "amd64" Family: "windows"
[DEBUG] Building Maven user-level plugin registry from: 'C:\Users\testuser\.m2\plugin-registry.xml'
[DEBUG] Building Maven global-level plugin registry from: 'C:\Users\testuser\apache-maven-2.2.1\bin\..\conf\plugin-registry.xml'
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[DEBUG] Loading plugin prefixes from group: org.apache.maven.plugins
[INFO] org.apache.maven.plugins: checking for updates from central
[DEBUG] Using Wagon implementation lightweight from default mapping for protocol http
[DEBUG] Checking for pre-existing User-Agent configuration.
[DEBUG] Adding User-Agent configuration.
[DEBUG] Connecting to repository: 'central' with url: 'http://repo1.maven.org/maven2'.
[DEBUG] Using Wagon implementation lightweight from default mapping for protocol http
[WARNING] repository metadata for: 'org.apache.maven.plugins' could not be retrieved from repository: central due to an error: Err
or transferring file: Permission denied: connect
[DEBUG] Exception
org.apache.maven.wagon.TransferFailedException: Error transferring file: Permission denied: connect
        at org.apache.maven.wagon.providers.http.LightweightHttpWagon.fillInputData(LightweightHttpWagon.java:143)
        at org.apache.maven.wagon.StreamWagon.getInputStream(StreamWagon.java:116)
        at org.apache.maven.wagon.StreamWagon.getIfNewer(StreamWagon.java:88)
        at org.apache.maven.wagon.StreamWagon.get(StreamWagon.java:61)
        at org.apache.maven.artifact.manager.DefaultWagonManager.getRemoteFile(DefaultWagonManager.java:546)
        at org.apache.maven.artifact.manager.DefaultWagonManager.getArtifactMetadata(DefaultWagonManager.java:443)
        at org.apache.maven.artifact.repository.metadata.DefaultRepositoryMetadataManager.resolve(DefaultRepositoryMetadataManager
.java:97)
        at org.apache.maven.plugin.DefaultPluginMappingManager.loadPluginMappings(DefaultPluginMappingManager.java:103)
        at org.apache.maven.plugin.DefaultPluginMappingManager.loadPluginMappings(DefaultPluginMappingManager.java:87)
        at org.apache.maven.plugin.DefaultPluginMappingManager.getByPrefix(DefaultPluginMappingManager.java:61)
        at org.apache.maven.plugin.DefaultPluginManager.getPluginDefinitionForPrefix(DefaultPluginManager.java:159)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.getMojoDescriptor(DefaultLifecycleExecutor.java:1801)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.segmentTaskListByAggregationNeeds(DefaultLifecycleExecutor.java:462
)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:175)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: java.net.SocketException: Permission denied: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:234)
        at sun.net.www.http.HttpClient.New(HttpClient.java:307)
        at sun.net.www.http.HttpClient.New(HttpClient.java:324)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
        at org.apache.maven.wagon.providers.http.LightweightHttpWagon.fillInputData(LightweightHttpWagon.java:115)
        ... 25 more
[INFO] Repository 'central' will be blacklisted
[DEBUG] Loading plugin prefixes from group: org.codehaus.mojo
[DEBUG] Skipping blacklisted repository central
[DEBUG] Skipping blacklisted repository central
[DEBUG] maven-archetype-plugin: using locally installed snapshot
[DEBUG] Artifact not found - using stub model: Unable to determine the latest version

  org.apache.maven.plugins:maven-archetype-plugin:pom:LATEST



[DEBUG] Using defaults for missing POM org.apache.maven.plugins:maven-archetype-plugin:pom:LATEST
[DEBUG] maven-archetype-plugin: using locally installed snapshot
[DEBUG] Artifact not found - using stub model: Unable to determine the release version

  org.apache.maven.plugins:maven-archetype-plugin:pom:RELEASE



[DEBUG] Using defaults for missing POM org.apache.maven.plugins:maven-archetype-plugin:pom:RELEASE
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] The plugin 'org.apache.maven.plugins:maven-archetype-plugin' does not exist or no valid version could be found
[INFO] ------------------------------------------------------------------------
[DEBUG] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: The plugin 'org.apache.maven.plugins:maven-archetype-plugin' does not exis
t or no valid version could be found
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.verifyPlugin(DefaultLifecycleExecutor.java:1569)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.getMojoDescriptor(DefaultLifecycleExecutor.java:1851)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.segmentTaskListByAggregationNeeds(DefaultLifecycleExecutor.java:462
)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:175)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.version.PluginVersionNotFoundException: The plugin 'org.apache.maven.plugins:maven-archetype-pl
ugin' does not exist or no valid version could be found
        at org.apache.maven.plugin.version.DefaultPluginVersionManager.resolvePluginVersion(DefaultPluginVersionManager.java:229)
        at org.apache.maven.plugin.version.DefaultPluginVersionManager.resolvePluginVersion(DefaultPluginVersionManager.java:91)
        at org.apache.maven.plugin.DefaultPluginManager.verifyPlugin(DefaultPluginManager.java:179)
        at org.apache.maven.plugin.DefaultPluginManager.loadPluginDescriptor(DefaultPluginManager.java:1642)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.verifyPlugin(DefaultLifecycleExecutor.java:1540)
        ... 15 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Fri Jan 06 20:02:54 IST 2012
[INFO] Final Memory: 2M/121M
[INFO] ------------------------------------------------------------------------



Solution: I tried almost everything found on net for almost 2 days, that did not work. Initially i was trying to build with maven 3.x version, seeing
this error, i switched to maven2.x, but still same error. Initially i was using jdk1.7, then i downgraded to jdk 1.6 but that also did not solve the problem.
My OS was win7 64 bit and i heard somewhere about java 32 bit and 64 bit. I downloaded both 32 and 64 bit java, modified all environment variables but still
same issue persisted. Somewhere i read about Windows Firewall enabling/disabling. I enabled/disabled Firewall, allowed java/http through allowed programs,
but that also did not fix the issue. I use a wireless router to route the network connection got from my ISP. I even removed that connection and tried to
use the IP directly by my system (since the router was giving me local IP such as 192.168.1.2 and i though that might be problem), but still same issue :(.
At last what fixed it, is interesting: I had purchased a new system which had Intel motherboard. There was in-built antivirus and Firewall installed with it.
When i disabled 'Personal Firewall' from that ESET security options. Then maven started downloading everything and built successfully.

Interestingly, this fixed other couple of issues: such as running IE 64 bit version and downloading files using torrent.

Monday, December 19, 2011

JAXB Exception: javax.xml.bind.PropertyException: name:


Explanation and Fix:


Sometimes using JAXB, you might need to write following output to the OutputStream object of browser. Intent is to show a minimal UI
on browser by using a XSL file and the XML attributes.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type='text/xsl' href='../myviewer.xsl'?>
<FileDate>
<DataName>file1.txt</DataName>
</FileData>

To implement this, in your Java code you will write something like this:
        StringWriter sw = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(FileDate.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", RESOURCE_XSL);
        marshaller.marshal(FileDate, sw);

        Console.WriteLine(sw.getBuffer().toString());
        response.setContentType("application/xml"); //response is object of HttpServletResponse.
        response.getWriter().print(sw.getBuffer().toString());

This works perfectly if you are using JAXB 2.2/ java6 or more. but if your system is confused between old and new java, then it throws this exception:
 Console.WriteLine(sw.getBuffer().toString()); -->will throw exception
javax.xml.bind.PropertyException: name: com.sun.xml.internal.bind.xmlHeaders value: <?xml-stylesheet type='text/xsl' href='../myviewer.xsl'?>

This exception is random and does not appear on all systems. To fix this, use try-catch block and try to use this: com.sun.xml.bind.xmlHeaders

so final code to correct this error will be:
try{
            marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", RESOURCE_XSL);
        }
        catch(PropertyException pex)
        {
            marshaller.setProperty("com.sun.xml.bind.xmlHeaders", RESOURCE_XSL);
        }

It should fix the issue.

Sunday, November 13, 2011

Securing or Encrypting Web.config file using command line

We will talk about securing a specific entry in Web.config file using the utilities that ASP.NET provides.
Prerequisites:
(1) Make sure you have information about your current Application's identity. You can know this by writing this code inside your code: "Current Application is running as: " + WindowsIdentity.GetCurrent().Name
(2) By clicking on the IIS management 'sites' tab, you will know the siteID of your application.
Steps:
(1) Modify web.config file to identify the key container (key container is where key will be stored after encryption)
Add This:

type="System.Configuration.RsaProtectedConfigurationProvider,
System.Configuration, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,processorArchitecture=MSIL"
keyContainerName="MyRSA"
useMachineContainer="true"/>
Here MyRSA is the keycontainername which will be used in future.
(2) You may wish to modify the configuration tag to let it recognize proper XML namespaces:
Otherwise web.config file may not recognize some type above
(3) Create key container on server:
e.g. C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis.exe -pc "MyRSA" -exp
Creating RSA Key container...
Succeeded!
it goes inside
<%ALLUSERSPROFILE%\Application Data\Microsoft\Crypto\RSA\MachineKeys> You can check the modified date
(4) Grant access to the key container. Access should be given to the application's identity. Application's identity is the one from prerequisite step 1 above. For me it was "IIS APPPOOL\DefaultAppPool"
e.g. C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -pa "MyRSA" "IIS APPPOOL\DefaultAppPool"
Adding ACL for access to the RSA Key container...
Succeeded!
(5) Same as 4 above:
e.g. C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -pa "NetFrameworkConfigurationKey" "IIS APPPOOL\DefaultAppPool"
Adding ACL for access to the RSA Key container...
Succeeded!
(6) Encrypt the entry inside Web.config file. I wanted to encrypt the connectionStrings section of my Web.config file.
e.g. C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -pe "connectionStrings" -app "/ui1" -site "1"
Encrypting configuration section...
Succeeded!
For me, siteid was 1 shown in IIS console. and 'ui1' was the alias name for my application1.
(7) If you want to encypt another UI app with same key, you can run again.
e.g. C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -pe "connectionStrings" -app "/ui2" -site "1"
Encrypting configuration section...
Succeeded!
You can see that siteID is same for both of the apps.

NOTE: 1. If you want to modify the username/passwords or other sensitive information inside Web.config file, decrypt the config file, edit/save and encrypt again.
Command to decrypt the connectionStrings:
run: aspnet_regiis.exe -pd "connectionStrings" -app "/ui1" -site "1"

for app2: aspnet_regiis.exe -pd "connectionStrings" -app "/ui2" -site "1"

After changing passwords, run step 6 and 7 again.

2. C# code automatically decrypts the Web.config file entries when application runs so no need to write extra code.

Hope this helps....

Friday, March 4, 2011

Android Error: alert dialog crashes during create() or return call

Suppose you launch your main activity named 'myMainActivity' which was mentioned as main activity in your manifest file. Then you navigate to a new page named 'myFiles' page using a new Intent(). Both of these classes extend Activity(). suppose there is a menu option on your 'myFiles' page which allows users to do some operation and based on user's response you have to show some alert dialog. First you will create menu items either using java code or XML file and load them inside. We are not going into that detail. We are going to discuss how to show alert dialog in this case. If you know the place where you need to show the dialog, then write this code there:
showDialog(int_value); //int_value is just a value which is user defined and we will use this to identify our dialog and also android will use this value while calling our dialog handlers.

override onCreateDialog(..) method :
*** If you use the help guides or documents to write this, you will write it as mentioned below and your program will crash while showing the dialog. ****

@Override
protected Dialog onCreateDialog(int id) {
AlertDialog alert = null;
switch( id ) {
case int_value:
AlertDialog.Builder builder = new AlertDialog.Builder(this); //remember it
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}});
alert = builder.create();
}
return alert; //THIS WILL CRASH

}

Reason: Reason is simple. AlertDialog.Builder() constructor expects the current activity's context where this dialog is running. If you give 'this', then it points to context which is for current intent and during show() method, when it tries to associate itself with 'this' pointer's activity, it fails to do so because that is not the main activity.

Solution: Go to your main activity java code. Put a static Context variable say 'myMainContext' and initialize it inside onCreate() method of main activity page. Use that static context variable while constructing your builder class.

So after modification the constructor will be like this: AlertDialog.Builder builder = new AlertDialog.Builder(myMainPage.myMainContext);

This should solve the problem.