Monday, November 25, 2013

Import Grails project from GitHub into Eclipse

  1. File -> Import -> Git -> Projects From Git > URI
  2. Enter the github repository URL. 
    You can get https URL from github repository page -> sidebar
  3. Select the branch you want to clone
  4. Select the local storage location for your new project
  5. Create a new groovy project. It will prompt you if you want to convert to grails project. Select yes.
    The location should be $ProjectRoot/src, the directroy that is the parent directory of grails-app!

Tuesday, May 28, 2013

How to change Eclipse to use spaces instead of tabs



For the default text editor:
  • General > Editors > Text Editors > Insert spaces for tabs (check it)
For PHP:
  • PHP > Code Style > Formatter > Tab policy (choose "spaces")
  • PHP > Code Style > Formatter > Indentation size (set to 4)
For CSS:
  • Web > CSS > Editor > Indent using spaces (select it)
  • Web > CSS > Editor > Indentation size (set to 4)
For HTML:
  • Web > HTML > Editor > Indent using spaces (select it)
  • Web > HTML > Editor > Indentation size (set to 4)
For XML:
  • XML > XML Files > Editor > Indent using spaces (select it)
  • XML > XML Files > Editor > Indentation size (set to 4)
For Javascript:
  • Javascript > Preferences > Code Style > Formatter > Edit > Indentation (choose "spaces only")
  • Rename the formatter settings profile to save it
For Java:
  • Java > Preferences > Code Style > Formatter > Edit > Indentation (choose "spaces only")
  • Rename the formatter settings profile to save it

To search for all the tabs in eclipse.
Search > File > search for containing text "\t" and select Regular expression > Search

To replace tab with spaces
Search > File > search for containing text "\t" and select Regular expression > Replace > Enter 4 spaces.

Wednesday, May 1, 2013

Regular Expression


Lookahead

  • q(?=u)  : quit
  • q(?!u)  : qtip  not quit


LookBehind

  • (?<=a)b  :  abc
  • (?<!a)b  :  xbc not ab



  • ^ matches at the start of the string
  • $ matches at the end of the string
  • \d matches a single character that is a digit
  • \w matches a "word character" (alphanumeric characters plus underscore)
  • \s matches a whitespace character
  • The dot matches a single character, except line break characters. It is short for [^\n] (UNIX regex flavors) or[^\r\n] (Windows regex flavors).gr.y matches graygreygr%y, etc.

Thursday, January 24, 2013

Spring 3.1 registering and using properties

Registering properties in Spring 

Starting with Spring 3.1, the new Environment and PropertySource abstractions simplify working with properties. The default Spring Environment now contains two property sources: the System properties and the JVM properties, with the System properties having precedence.

(1) Registering Properties via the XML namespace:
  <context:property-placeholder location="classpath:config/${env:local}/**/*.properties" ignore-unresolvable="true"/>  

(2) Registering Properties via Java Annotation:(2) 
 @PropertySource("classpath:config/${env:local}/environment.properties")  

Spring 3.1 introduces the new @PropertySource annotation, as a convenient mechanism of adding property sources to the environment. But as opposed to using XML namespace element, the Java @PropertySource annotation does not automatically register a PropertySourcesPlaceholderConfigurer with Spring.

Instead, the bean must be explicitly defined in the configuration to get the property resolution mechanism working.
   @Bean  
   public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {  
   return new PropertySourcesPlaceholderConfigurer();  
   }  

Note: Resource location wildcards (e.g. **/*.properties) are not permitted; each location must evaluate to exactly one .properties resource.  So if you want to use wildcards in your propertySource, don't  use Method 2).

(3)  Registering Properties via Java configuration:
   static @Bean public PropertySourcesPlaceholderConfigurer myPropertySourcesPlaceholderConfigurer() throws IOException {  
     String envVar = System.getenv("env");   
     if (envVar == null)   
       envVar = "local";  
     PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();  
     p.setLocations(new PathMatchingResourcePatternResolver()  
             .getResources("classpath*:config/"+envVar+"/*.properties"));  
     p.setIgnoreUnresolvablePlaceholders(true);  
     return p;  
   }  

Using properties in Spring 

 Both the older PropertyPlaceholderConfigurer and the new PropertySourcesPlaceholderConfigurer added in Spring 3.1 resolve ${…} placeholders within bean definition property values and @Value annotations.

For example, to inject a property using the @Value annotation:
 @Value( "${jdbc.url}" )  
 private String jdbcUrl;  

Using properties in Spring XML configuration:
 <bean id="dataSource">  
  <property name="url" value="${jdbc.url}" />  
 </bean>  

And lastly, obtaining properties via the new Environment APIs:
 @Autowired  
 private Environment env;  
 ...  
 dataSource.setUrl(env.getProperty("jdbc.url"));  




Friday, January 18, 2013

MongoDB


Default
 <mongo:mongo write-concern="SAFE" />  
 <mongo:db-factory id="mongoDbFactory"  
        host="localhost"  
        port="27017"  
        dbname="database"  
        username="joe"  
        password="secret"/>  
 <bean id="mongoTemplate">  
      <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>  
 </bean>  

 mongo = new Mongo();  
 mongoDbFactory = new SimpleMongoDbFactory(mongo, "dbname");  
 mongoTemplate = new MongoTemplate(mongoDbFactory());  

Spring 3.2 Java Configuration For SpringMvc

 @Configuration  
 @EnableWebMvc  
 @Import({DbConfig.class, LoggingConfig.class})  
 @ComponentScan(basePackages = {"org....", "org...."})  
 public class WebConfig extends WebMvcConfigurerAdapter {    
   @Bean  
   public ViewResolver viewResolver() {  
     InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
     resolver.setPrefix("WEB-INF/views/");  
     resolver.setSuffix(".jsp");  
     return resolver;  
   }  
   @Override  
   public void addResourceHandlers(ResourceHandlerRegistry registry) {  
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");  
   }  
 }  

The new @EnableWebMvc : The functionality of the annotation is equivalent to the XML version:
<mvc:annotation-driven />


 @RunWith(SpringJUnit4ClassRunner.class)  
 //defaults to "file:src/main/webapp"  
 @WebAppConfiguration  
 //detects "${classname}-context.xml" in same package or static nested @Configuration  
 //The default loader is AnnotationConfigWebContextLoader.class  
 @Configuration class  
 @ContextConfiguration(classes=WebConfig.class)  
 @Category(IntegrationTest.class)  
 public class WebIntegrationTest {  
   @Autowired  
   protected WebApplicationContext webApplicationContext;  
   protected MockMvc mockMvc;  
   @Before  
   public void setUp() {  
     mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();  
   }  
   @Test  
   public final void whenSpringContextIsInstantiated_thenNoExceptions(){  
   }  
 }