IT Certificaions

Google Cloud Platform

Tuesday, December 16, 2014

Generate Random file name in Java

You can create a random string for a file name using following code snippet.

public static String getRandomString(String fileName) {
char fileNameArray[] = fileName.toCharArray();
int fileNameLen = fileNameArray.length;
int c = 'A';
int r1 = 0, z=0;
StringBuilder pw = new StringBuilder("");
for (int j = 0; j < 25; j++) {
r1 = (int) (Math.random() * 4);
switch (r1) {
case 0:
c = '0' + (int) (Math.random() * 9);
if (c>'9'){
z = reset(z,fileNameLen);
c = fileNameArray[z];
}
break;
case 1:
c = 'a' + (int) (Math.random() * 25);
if (c<'a' && c>'z'){
z = reset(z,fileNameLen);
c = fileNameArray[z];
}
break;
case 2:
c = 'A' + (int) (Math.random() * 25);
if (c<'A' && c>'Z'){
z = reset(z,fileNameLen);
c = fileNameArray[z];
}
break;
}
pw.append((char) c);
}
fileNameArray = null;
String s="";
try{
s =fileName.substring(fileName.lastIndexOf(".")); 
if (s.equalsIgnoreCase(".tmp")){
s=".jpg";
}
}catch(ArrayIndexOutOfBoundsException arrayExp){
s="";
}
return new String(pw+s.trim());
}

private static int reset(int z, int len){
if (z>=len){
z=0;
}
return z;
}

Wednesday, December 10, 2014

Tomcat 7: Set context path for your application in tomcat 7

Set context path for your application in tomcat 7

If you want to change context path for your application, you can do by changing
{catalina_base}/conf/server.xml file.
Suppose, you have deployed your application in tomcat 7,
              http://localhost:8080/MyApp
You want to set context path to newMyApp,
Modify, host element in server.xml under tomcat/conf

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="false">
....
... .
<Context path="/ newMyApp "  docBase="/ MyApp " reloadable="true">
 </Context>



 </Host>

For detailed information, visit http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html#A_word_on_Contexts

Maven :: Include servlet-api only at local maven install not during building war

Include  servlet-api  only at local maven install not during building war
Add scope element with value as ‘Provided’. Now maven will not include this jar in lib whiling

building war file so that you can deploy it on tomcat.

 <dependency>
 <groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

For all scopes in maven, please visit http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

JAXB:GlobalBinding :: cvc-complex-type.2.4.b: The content of element 'jaxb:globalBindings' is not complete

Error ::
 cvc-complex-type.2.4.b: The content of element 'jaxb:globalBindings' is not complete. One of '{"http://

 java.sun.com/xml/ns/jaxb":javaType, "http://java.sun.com/xml/ns/jaxb":serializable, WC[##other:"http://

 java.sun.com/xml/ns/jaxb"]}' is expected.


Solution :

  Check  <jaxb:globalBindings> tag in your bindings.xml, you may be missing some required tag (If you are using latest eclipse like eclipse-Luna or latest).
 Add this tag if missing,

<jaxb:globalBindings>
            <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" 
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" 
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
            <xjc:noValidator />

    </jaxb:globalBindings>