Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Thursday, July 11, 2013

JAVA: The filename, directory name, or volume label syntax is incorrect

While doing file copy operation using java, if we come across an error saying

"The filename, directory name, or volume label syntax is incorrect"

then check the source filename which we are trying to copy and ensure there is no white space before or after the filename.

Better to use fileName.trim() before trying to copy

Tuesday, July 2, 2013

How to get the list of partitions of em using java - SOA 11g



The following java code will  get all the partitions available in em


public static ArrayList getPartitions(String host,String port,String username,String password) throws Exception
{
 System.out.println("Trying list all partitions in "+host);
  ArrayList partitions = new ArrayList();
    boolean found = false;

String contextFactory = "weblogic.jndi.WLInitialContextFactory";

String mbeanRuntime = "weblogic.management.mbeanservers.runtime";
String jmxProtoProviderPackages = "weblogic.management.remote";

String mBeanNamePartition = "oracle.soa.config:Application=soa-infra,j2eeType=FolderLifecycleConfig,name=soa-infra";
MBeanServerConnection mbsc = TestComposite.getMbeanServerConnection(host, port, username, password,mbeanRuntime,jmxProtoProviderPackages);


ObjectName mbeanpartition = new ObjectName(mBeanNamePartition);

Object FolderArray = mbsc.getAttribute(mbeanpartition, "Folders");
System.out.println("FolderArray ="+FolderArray);
Folder[] folderData = (Folder[]) FolderArray;
for (int i = 0; i < folderData.length; i++) {
System.out.println("Folder Data"+folderData[i].getName());
partitions.add(folderData[i].getName());
}


System.out.println("partitions size"+partitions.size());
return partitions;

}

Wednesday, November 7, 2012

Read file content. Write content to file

Simple utility methods for reading from and writing to a file


public static String readFile(File file) throws Exception
{
 BufferedReader br = null;
 java.io.FileReader fr = null;
 try {
 fr = new java.io.FileReader(file);
 br = new BufferedReader(fr);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
throw new FileNotFoundException(e.getMessage());
}
StringBuffer sb = new StringBuffer();
String str= null;
try
{
while((str=br.readLine()) != null)
{
 sb.append(str);
 sb.append("\n");
}

}
    catch(Exception e)
{
e.printStackTrace();
throw new Exception(e.getMessage());
}
    finally
    {
    try {
    fr.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
    return sb.toString();
  }
public static void writeFile(File file,String content) throws Exception
{
BufferedWriter out=null;
     try{
       // Create file
       FileWriter fstream = new FileWriter(file);
           out = new BufferedWriter(fstream);
       out.write(content);
       //Close the output stream
       out.close();
       }catch (Exception e){//Catch exception if any
         System.err.println("Error: " + e.getMessage());
         throw new Exception(e.getMessage());
       }
       finally
       {
        if(out != null)
        {
        try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        }
       }
}

Remove namespace from xml

Sometime we might need to remove all the namespaces from an xml document.

Here is piece of java code which does that job !!


public static String removeNamespaces(String s)
 {
     Transformer transformer = null;
     try
     {
         transformer = templates.newTransformer();
     }
     catch(TransformerConfigurationException transformerconfigurationexception)
     {
         System.err.println("Error transforming XML");
         transformerconfigurationexception.printStackTrace();
         return null;
     }
     StringReader stringreader = new StringReader(s);
     StringWriter stringwriter = new StringWriter();
     try
     {
         transformer.transform(new StreamSource(stringreader), new StreamResult(stringwriter));
         return stringwriter.toString();
     }
     catch(TransformerException transformerexception)
     {
         System.err.println("Error transforming XML");
         transformerexception.printStackTrace();
         return null;
     }
 }