Skip to main content

Posts

Showing posts from January, 2015

How to convert the Base of a Decimal Number in Java

Points To Remember You can convert the number from decimal to any base by dividing the number by the base till the number is either 0 or less than the base it self and counting the remainders in a reverse order. Program : Convert the base of a Decimal Number import java.util.Scanner; class ConvertBase{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter the Original NUmber"); int originalNumber = sc.nextInt(); System.out.println("Enter the base for conversion"); int base = sc.nextInt(); String convertedNumber = new ConvertBase().convert(originalNumber,base); System.out.println("Original Number = "+originalNumber); System.out.println("Converted NUmber = "+convertedNumber); } public String convert(int original, int base){ String number = ""; String converted =""; while(original != 0){ int digit = original % base; original /= bas

Spring Security : Custom UserDetailsService and Custom UserDetails

Points To Remember You need to change the UserDetailsService, User Object of the spring security to achieve this. Add Custom User Details to Spring Security Authentication Object First of all we nee to create a new User Object that will override the User class of the spring security in package  org.springframework.security.core.userdetails.User  After this, you need to tell  UserDetailsService to use this object as the principal for the authentication token. For this you will have to override the UserDetailsService of the spring security. You can have the custom User object like. Custom User Class -> MyUser.groovy package com.ekiras import org.springframework.security.core.GrantedAuthority /** * Created by ekansh on 24/1/15. */ class MyUser extends org.springframework.security.core.userdetails.User { // Declare all custom attributes here private final Object id; private String name; public MyUser(String username, String password, boolean enabled, boolean accountNon

Allegations and Mixtures

Concepts Examples How To Solve Allegations Suppose the average weight of a group of objects O 1 is A 1 and average weight of group of objects O 2 is A 2 and A 1 < A 2 , then we can write the data in the manner as shown in the figure. (A w −A 2 ) : (A w −A 1 ) gives the ratio of O 1 : O 2 . Allegations does not give the actual volume to be mixed but only the ratio in which volumes are to be mixed. Question 1 A shopkeeper sells two types of rice A and B. If he sold 30% of type A rice at a profit of 50% and sold 90% of the rice of type B at a profit of 10%. What is the averge profit percent of the shopkeeper, if he sells only these two types of rice. 35%

Weighted Averages

Concepts Examples When we have to calculate the average of some groups of different things, having different number of elements is being calculated, then it is called the weighted average. If the number of elements in n different groups be N 1 ,N 2 ,N 3 .. N n and their respective averages be A 1 ,A 2 ,A 3 ...A n , then Weighted average = $\frac{\mathbb{N}_1\cdot A_1+\mathbb{N}_2\cdot A_2+\mathbb{N}_3\cdot A_3...\mathbb{N}_n\cdot A_n}{\mathbb{N}_1+\mathbb{N}_2+\mathbb{N}_3...\mathbb{N}_n}$ N 1 · A 1 + N 2 · A 2 + N 3 · A 3 . . . N n · A n N 1 + N 2 + N 3 . . . N n   Question 1 Average cost of buying 5 quality A crackers is 60 and cost of buying 10 quality B crackers is 80. WHhat will be average cost of buying all the crackers. 71.33

Spring Security : Create a Custom Authentication Filter

Points To Remember You may need to create an AuthenticatioFilter when you want to create a custom logic for handling the authentication filter. You may also want to create your own Authentication Provider, Entry Point, Authentication Token etc to customize the authentication process to a new level. Step 1 : Create a Filter Let us first create a class named MyAuthenticationFilter and then register it as a bean in resources.groovy . After we have created the class and registered the it as a bean, we can use this class as a filter for our custom spring security authentication. Class : MyAuthenticationFilter.groovy package com.ekiras import org.springframework.context.ApplicationEventPublisher import org.springframework.security.authentication.UsernamePasswordAuthenticationToken import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent import org.springframework.security.core.Authentication import org.springframework.security.core.AuthenticationExceptio

How to integrate Spring Security in Grails

Points To Remember Go to the Grails Spring Security Plugin  and add the dependency in the BuildConfig . Integrate Spring Security in Grails Add the latest grails spring security plugin in the build config of the project. This will add spring security jars and classes that will be used to configure spring security in the project. http://grails.org/plugin/spring-security-core Now run the following command from the terminal. grails s2-quickstart com.ekiras User Role Here the syntax of the above command is grails s2-quickstart {package} {user domain} {authority domain} The above command will create three Domains in the project User, Role and UserRole. user domain will contain the user info, role domain will contain Authorities and UserRole will contain the user authority mappings. It will also add the following settings to the Config.groovy // Added by the Spring Security Core plugin: grails.plugin.springsecurity.userLookup.userDomainClassName = 'com.ekiras.User' grails.plugin.spri

What is ServletConfig in Java J2EE application

Points To Remember Each Servlet has its own ServletConfig object. ServletConfig is defined in web.xml ServletConfig is a way of giving init parameters to a servlet during initialization.  ServletConfig is a predefined interface. ServeltConfig object is created by the container when a servlet is initialized and this object remains with the servlet throughout the life cycle of the servlet. Example : How to get ServletConfig Object in a Servlet. Lets take an example where we will add a few init params in the servlet config and get the config object from the servlet config object within the servlet. package com.ekiras; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletConfigTest extends HttpServlet { private static final long seri

How to create a Custom Annotation in Java

Points To Remember An annotation can be used as a replacement of interfaces. You can make both empty/marker annotations and annotations with methods. You can define the retention policy for the annotation and where the annotation can be used. Program : Create A Custom Annotation The following program creates a custom empty annotation. import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Table { public String name() default "table"; } The above class can be used as a custom annotation with only one method name with a default value. This annotation can be applied only to the class i.e it is class level annotation and can not be applied to any member of a class. The following program creates a custom annotation with methods defined. import java.lang.annotation.ElementType; import java.lang.ann

How to find all the packages in a Project with a Prefix

Points To Remember You to need to add the Reflections jar in the class path.  You will not be able to load the packages other than the current package using Package.getPackages()  since current class loader will not be able to reach them. Program : Scan all packages with a Prefix You can use  Package.getPackages()  approach to find all the packages that are in the current class loader. But you will not be able to get the packages that are not in the class path of the current class loader. So you need to include the jar "Reflections"  to do this for you. All the dependency to the project via pom.xml  or you can add the jar in the class path of the project. The following code will list all the packages in the project that start with the prefix in the project. public static Set<String> findAllPackagesStartingWith(String prefix) { List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>(); classLoadersList.add(ClasspathHelper.contextClassLoad