Posts

Get the Recurrence of Days Randomally

I faced a problem where I had to generate random picks in a week so that it  can include permutations of all days in a week, in every possible manner. Computationally it creates following possibilities, plus one more fora none condition P = 7C1 + 7C2 + 7C3 + 7c4 + 7C5 + 7C6 + 7C7    = 7 + 21 + 35 + 35 + 21 + 7 + 1    = 127 So 128 conditions including 1 none of all condition. Where nCk is computed as   n!/((n-k)!*k!) or (n (n-1) (n-2) .... (n-k+1))/ (k (k-1) (k-2)........1) So going by traditional programming it will take 128 if else conditions or switch cases to generate a random sample. This will grow exponentially if the value of 'n' increases in case the requirement changes to some thing. The following method returns a string of a possible random sample out of these 128 probabilities. Here days are marked as follows. Sunday as 0 Monday as 1 Tuesday as 2 Wednesday as 3 Thursday as 4 Friday as 5 Saturday as 6 The ou...

Eclipse - Maven project dependencies not in classpath

Image
While configuring a new maven project one may face problem "Maven project dependencies, not in classpath". There are some steps one need to take care while configuring maven project in their workspace. This error is related to workspaces abiding by JAVA 1.4, so it will be very rare that somebody finds this issue. 1. Check out the project from the repository like svn, cvs, git etc. 2. Right click on project and go to Configure > Convert to Maven Project 3. If the project is still not initialized, right-click on the project and go to project properties and go to Project Facets . 4. Apply the facets as per project, don't forget to click on Java facet. 5. Make sure that version is 1.5+ for Java. By default, many eclipse flavours take it default as Java 1.4. This causes Maven dependencies not to move in application classpath, and hence many libraries will be missing, and your project will keep throwing the compilation error. 6. If still project is not showing maven ...

The security hole left with JNDI for server resources

In Java world, JNDI (Java Naming and Directory Interface) is one very common method for applications to access server resources like data sources, EJB's, JMS queues, file stores etc. The security risk is that: Not only JNDI can be called by applications hosted in the same container, but also remotely. In an organization, application server admins do not take care of security risks unknowingly or neglect by assuming they or on a secure LAN. This exposes the resources to grave risk as anyone within LAN can access unauthorized data without being detected and abuse the system. ctx = null ; Properties env = new Properties (); env . put( Context . INITIAL_CONTEXT_FACTORY , "CONTEXT_FACTORY" ); env . put( Context . PROVIDER_URL , "CONTEXT_PROVIDER_URL" ); DataSource datasource = ( DataSource )initialContext . lookup( "DATASOURCE_CONTEXT_NAME" ); try { ctx = new InitialContext (env); Connection conn = datasource . getConnection(); //...

illegal reference to static field from initializer

I found an issue with build for one my projects, this project was working fine on my IDE (eclipse). When i used build tool i encountered this error. [echo] Compiling the source files .....     [javac] Compiling 54 source files to /comp/acserver/slaves/node/workspace/build/HelloWorld/build     [javac] /comp/acserver/slaves/node/workspace/src/com/pkg/config/Person1.java:32: illegal reference to static field from initializer     [javac]         logger=Logger.getLogger(CustomLogs.getLogName(Person1.class));     [javac]                                                                    [jav...

eclipse not publishing again to tomcat server after removing and adding back the project

Image
Many a time it happens that after removing a project from eclipse embedded server, once you try add back, the application does not get added properly. Some of the contents go missing while removing then adding adding the application back while eclipse publishes the application content into servers directory. This causes 404 error on browser as the application is not loaded inside the container. To avoid this situation, proceed as following 1. Goto add/ remove option by right clicking on the server. 2. Select remove on subsequent screen. 3. Clean the server Thereafter use option (1) to add back the application. This will ensure that application content are published successfully to server directory.

sun access manager, session not cleaned issue. Identity theft and impersonisation.

A high-security risk, leading to identity theft and impersonation. This security issue is mostly caused by lack of knowledge of security aspects on part of application developers/ architects. PROBLEM One person signs into a sun access manager policy-enabled application from a web browser. After completing his task, he signs out and goes home relieved of his/ her work. But this machine is in public access, say some kiosk or cyber cafe. Another user comes in and signs into the same application which the previous user used. Viola....he logs into an application as the first person instead. CAUSE Sun access manager is responsible for deleting cookies of the application, which stores session and may store some other credentials too. Now one can only delete the cookies that belong to it. So in case, Sun access manager agent resides in www.xyz.com domain (see the site URL in the address bar for respective sites), it cannot delete cookies from another application from www.abc.com ...

Trim zeros from decimal string

I came across one interesting requirement, where I had to clip zero's from the decimal formatted string. Sharing the code below. Requirement X.Y000 as X.Y 0.1000 as 0.1 1.000 as 1 Java method: private static final Pattern TRAILING_ZERO = Pattern. compile ( "[.]{1}[0-9]*(0)+$" );        public static String removeTrailingZeros(String digit){               if (digit== null ) return digit;               Matcher m= TRAILING _ZERO .matcher(digit);               if (m.find()){                      digit=digit.replaceAll( "0*$" , "" );                 ...