Category: Uncategorized

  • TransactionRequiredException: Executing an update/delete query

    Simply adding @Transactional on the method will resolve this issue

  • SQL Rounding Error when calculating percentages

    For cross compatability between sql and mysql servers when we do a division on a aggregated variable like count
    we need to promote the INT to a DOUBLE type by multiplying it by 1.00 otherwise our results will loose precision

    Examples


    1/2=0 Incorrect Results
    1.00/2=.5 or 1.00/2.00=.5 Good

    For sake of consistency both variables have been promoted to DOUBLE


    SELECT ROUND((COUNT(FieldA) * 1.00) / (COUNT(FieldB) * 1.00) * 100, 0)
    FROM TESTTABLE

  • Detecting Wide Area Network(WAN) IP programmatically

    Detecting Wide Area Network(WAN) IP programmatically

    This is a reliable way to detect WAN IP address from Java, cpp or any other programming language. Motivation for this was my need to update my DNS server automatically, presented code will be in Java I have decided to use Java for cross-platform support and its my favorite language, in near feature I will include cpp code as well(moding Linksys router to Support xname.org Free DNS service)

    Intro

    There are couple ways of detecting an WAN IP address that I know of, first one is to use ping utility with hop count of 1 then parse the results

    Z:\>ping -r 1 www.google.com

    Pinging www.l.google.com [64.233.167.147] with 32 bytes of data:

    Request timed out.

    drawback of this approach is that it is dependent on ping utility and is not cross platform compatible. The second strategy is to use some external service that will

    echo back us our ip address like http://www.whatismyip.com/ problem with this approach is that if service is down we want be able to retrieve our IP.

    Strategy

    I decided to go with second option using external service, to overcome the possibility of service being down I have assembled list of number of free Ip detection services using Goggle. Next step was to parse the page and extract the IP address, here came up another problem, some of the services contained multiple IP’s. My solution was to query my services while the number of top IP count was less than a set threshold.

    Code

    I am using regular expressions to extract all the IP from retrieved page.

    Regex Here

  • Fixing JasperReports net.sf.jasperreports.engine.JRRuntimeException: Error creating SAX parser

    While upgrading machines I have upgraded to run jdk 1.6 which have screwed up compiling of my reports with following error.

    C:\Program Files (x86)\Java\jdk1.6.0_24\jre\lib\endorsed
    Source File:c:/Users/greg/report2.jrxml
    Exception in thread "main" net.sf.jasperreports.engine.JRRuntimeException: Error creating SAX parser
    	at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.createParser(JRReportSaxParserFactory.java:109)
    	at net.sf.jasperreports.engine.xml.JRXmlDigesterFactory.createParser(JRXmlDigesterFactory.java:1320)
    	at net.sf.jasperreports.engine.xml.JRXmlDigesterFactory.createDigester(JRXmlDigesterFactory.java:1295)
    	at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:199)
    	at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:164)
    	at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:148)
    	at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:85)
    	at com.americanbanksystems.complianceproreports.util.CompileReport.compileReport(CompileReport.java:133)
    	at com.americanbanksystems.complianceproreports.util.CompileReport.main(CompileReport.java:189)
    Caused by: org.xml.sax.SAXNotRecognizedException: http://java.sun.com/xml/jaxp/properties/schemaLanguage
    	at gnu.xml.aelfred2.XmlReader.getProperty(XmlReader.java:181)
    	at gnu.xml.aelfred2.XmlReader.setProperty(XmlReader.java:166)
    	at gnu.xml.aelfred2.JAXPFactory$JaxpParser.setProperty(JAXPFactory.java:147)
    	at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.configureParser(JRReportSaxParserFactory.java:140)
    	at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.createParser(JRReportSaxParserFactory.java:104)
    	... 8 more
    

    My first thought was to place the xalan.jar inside the ‘java.endorsed.dirs’ but that did not fix the problem.

     System.out.println(System.getProperty("java.endorsed.dirs"));

    So after a little investigation I have found the problem and simple solution, download following file or extract it from jasper reports libs folder

    xercesImpl-2.7.0.jar
    

    And place it inside your “java.endorsed.dirs” thats all.

  • Set memory for Sonatype Nexus

    While starting Nexus I was getting following error, this was on a machine that was running at 1GB of memory.

    Error occurred during initialization of VM
    Could not reserve enough space for object heap

    So the solution was to edit /usr/local/nexus/bin/jws/wrapper.conf
    and add wrapper.java.additional.4=-Xmx128m parameter this setup java to use only 128mb of memory for the new VM.

    Solution 2

    After further analysis of the config file there are following options that can be set as well, just need to uncomment them

    # Size Java memory, in MB (-Xms)
    #wrapper.java.initmemory=128
    # Size Java memory, in MB (-Xmx)
    #wrapper.java.maxmemory=256

  • Invoking Webservice Methods from Java

    This is my helper method for invoking webservices from java. One bonus perk is that this handles the exception when we are using self signed certificates by providing our own HostnameVerifier. This could be more generalized but all I needed was to invoke this one server.

    	/**
    	 * Invoke webservice to retrive data
    	 * 
    	 * @param 
    	 *            Return type the service will return
    	 * @param T
    	 * @param methodName
    	 *            name of method to execute
    	 * @param methodArgument
    	 *            need to match the methods in the webservice
    	 * @return
    	 */
    	public  T invokeWebserviceService(Class T, String methodName,
    			Object... methodArgument) {
    		T result = null;
    		try {
    			HttpServletRequest req = getRequest();
    			String host = req.getServerName();
    			String schema = "http";
    			if ("https".equals(req.getScheme())) {
    				schema = "https";
    				// Need to overide HostnameVerifier otherwise it throws a
    				// exception when we are using selfsigned certificates
    				HostnameVerifier hv = new HostnameVerifier() {
    					public boolean verify(String urlHostName, SSLSession session) {
    						return true;
    					}
    				};
    				HttpsURLConnection.setDefaultHostnameVerifier(hv);
    			}
    
    			String wsdlURL = schema + "://" + host
    					+ "/webservices/RiskBean"
    					+ schema.toUpperCase() + "?wsdl";
    			URL url = new URL(wsdlURL);
    
    			String ns = "http://reporting.ws.com/";
    			QName qname = new QName(ns, "RiskService"
    					+ schema.toUpperCase());
    			QName port = new QName(ns, "RiskBean"
    					+ schema.toUpperCase() + "Port");
    			QName operation = new QName(ns, methodName);
    
    			ServiceFactory riskServiceFactory = ServiceFactory.newInstance();
    			Service riskService = riskServiceFactory.createService(url, qname);
    
    			Call call = riskService.createCall(port, operation);
    			result = (T) call.invoke(methodArgument);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return result;
    	}
    

    Hopefully someone finds this helpful.

  • Detaching managed entity from entitymanager.

    This code will detach a entity from hibernate Entitymanger making it thus unmanaged entity.

    // Prepare workbook object to be used for cloning.
     Session session = (Session) entityManager.getDelegate();
     session.evict(workbook); // same as detached
     workbook.setId(null);
    
  • Ant BuildException Cannot run program “..\src\protoc”

    Error that I am getting when trying to run mvn test on google protobuf library.

    [INFO] Executing tasks
    [INFO] ------------------------------------------------------------------------
    [ERROR] BUILD ERROR
    [INFO] ------------------------------------------------------------------------
    [INFO] An Ant BuildException has occured: Execute failed: java.io.IOException: Cannot run program "..\src\protoc": CreateProcess error=2, The system c
    annot find the file specified
    
    [INFO] ------------------------------------------------------------------------
    [INFO] For more information, run Maven with the -e switch
    

    I did place protoc.exe in the src directory. As a side note this is running on windows machine.

    Quick solution that I found was to edit the pom.xml file and modify the Ant tast into

          
                    
    
  • Ant BuildException Cannot run program “..\src\protoc”

    Error that I am getting when trying to run mvn test on google protobuf library.

    [INFO] Executing tasks
    [INFO] ------------------------------------------------------------------------
    [ERROR] BUILD ERROR
    [INFO] ------------------------------------------------------------------------
    [INFO] An Ant BuildException has occured: Execute failed: java.io.IOException: Cannot run program "..\src\protoc": CreateProcess error=2, The system c
    annot find the file specified
    
    [INFO] ------------------------------------------------------------------------
    [INFO] For more information, run Maven with the -e switch
    

    I did place protoc.exe in the src directory. As a side note this is running on windows machine.

    Quick solution that I found was to edit the pom.xml file and modify the Ant tast into

          
                    
    
  • SQL Injection Presentation for ISSA

    This is a PowerPoint of presentation I gave for ISSA group in Oklahoma City, OK

    Here is listing of assets used during the presentation