Monday, November 26, 2018

Node JS installation behind firewall with ntlm authentication.

Strangely enough the ntlmaps.exe didn't work well with Nodejs.  Downloading of the dependencies and javascripts fails.

Quick solution for this was using px-proxy pyhon based proxy.

You can Install it with:

pip install px-proxy

and run it with:

px --proxy=X.X.X.X:port 

where X.X.X.X is Ip address of your firewall and port is the port used.

The catch here was that to use pip ... i used ntlmaps.exe  (see one of my previous posts)

So, you need a ntlm proxy (ntlmaps.exe)  to download ntlm proxy (px-proxy) behind firewall ;-)

Thursday, November 15, 2018

Glassfish 4.1.1 Rest Missing Library

When trying to Implement Rest ApI using jersey on Glassfish 4.1.1 the following exception in

thrown:
 java.lang.ClassNotFoundException:        com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector
not found by com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider


This is due to missing library jackson-module-jaxb-annotations-2.5.4.jar in

 C:\Program Files\glassfish-4.1.1\glassfish\modules

 Putting it there solves the problem.

 Thanks to this blog

 https://javaeeblog.wordpress.com/2016/06/21/jackson-glassfish-4-1-1/


Sunday, June 17, 2018

Run firexox in single process mode


To disable multiprocess go to "about:config" by typing it in your URL bar.
Search for browser.tabs.remote.autostart using the search box on about:config.
There may be multiple results.
Set them all to false and restart the browser

Wednesday, December 13, 2017

Showing build time, version etc in web apps and desktop applications


It is quite convenient to be able to show Application Version and build time in any given java program. Below is my way of doing this. 

Ant script for War Files 
<target name="-post-dist">      
        <!-- Empty placeholder for easier customization. -->
        <!--<property file="build.properties"/>-->
        <buildnumber file="build.version"/>
        <tstamp>
            <format property="timestamp" pattern="yyyy-MM-dd HH:mm:ss"/>
        </tstamp>
            <jar destfile="${dist.war}" update="true">
     
        <manifest>
            <attribute name="Manifest-Version" value="1.0" />        
            <attribute name="Created-By" value="Me" />        
            <attribute name="Bundle" value="${build.number}"/>
            <attribute name="Bundle-Date" value="${timestamp}" />
        </manifest>
        </jar>
        <!-- You can override this target in the ../build.xml file. -->
 </target>
Java Code for Web Application 

 public static String getBuildNumberAndDateFromWar(WebServiceContext context){
        String buildNumber="";
        try {
            javax.xml.ws.handler.MessageContext mc = context.getMessageContext();
            HttpServletRequest req = (HttpServletRequest) mc.get(javax.xml.ws.handler.MessageContext.SERVLET_REQUEST);
            InputStream stream = req.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
            Manifest manifest = new Manifest(stream);
            Attributes attributes = manifest.getMainAttributes();
            String bundle = attributes.getValue("Bundle");
            String bundle_date = attributes.getValue("Bundle-Date");
            stream.close();
            if (bundle!=null && !"null".equals(bundle))
                buildNumber = " [bundle:"+ bundle + ", date:" + bundle_date+"] ";

        } catch (Exception e) {
            e.printStackTrace();
        }
        return buildNumber;
    }

Ant Script for Jar file

 <target name="-post-jar">
        <!-- Empty placeholder for easier customization. -->
        <!-- You can override this target in the ../build.xml file. -->
        <buildnumber file="build.version"/>
        <tstamp>
            <format property="timestamp" pattern="yyyy-MM-dd HH:mm:ss"/>
        </tstamp>
            <jar destfile="${dist.jar}" update="true">
     
        <manifest>
            <attribute name="Manifest-Version" value="1.0" />        
            <attribute name="Version" value="1.0.1" />
            <attribute name="Created-By" value="Me" />        
            <attribute name="Bundle" value="${build.number}"/>
            <attribute name="Bundle-Date" value="${timestamp}" />
        </manifest>
        </jar>
 </target>

Java code for Java Application 

  public static  String getVersionBuildNumberAndDateFromJar(){
        String buildNumber="";
        try {
            InputStream stream = (new EscrowPaymentsAppClient()).getClass().getResourceAsStream("/META-INF/MANIFEST.MF");
            Manifest manifest = new Manifest(stream);
            Attributes attributes = manifest.getMainAttributes();
            String version = attributes.getValue("Version");
            String bundle = attributes.getValue("Bundle");
            String bundle_date = attributes.getValue("Bundle-Date");
            stream.close();
            if (bundle!=null && !"null".equals(bundle))
                buildNumber = " [version:"+ version + ", bundle:"+ bundle + ", date:" + bundle_date+"] ";

        } catch (Exception e) {
            e.printStackTrace();
        }
        return buildNumber;
    }

Tuesday, December 12, 2017

Using Python's pip behind Firewall (on Windows)

To use python behind a firewall that uses ntlm authentication we will use ntlm maps local proxy.

So here are the steps:

1. Download and install ntlm maps 
   (local proxy that will handle the authentication with our corporate proxy)
    http://ntlmaps.sourceforge.net/

2. Configure server.cfg of the ntlm maps,  change the following
      LISTEN_PORT:5865
      PARENT_PROXY:XXXX.XXXX.XXXX.XXXX
      PARENT_PROXY_PORT:XXXX
      NT_DOMAIN:xxxxx.xxx
      USER:xxxxxxxxxxx
      PASSWORD: 

   If we leave the password blank it will ask us for it at run-time.

3. if ntlm maps does not run - then some dlls are missing those are
    -MSVCP71.DLL
    -MSVCR71.DLL
    Copy then into SysWOW64 if needed (on 64 bit system) or into (System32 on 32 bit system)

4. run the ntlm maps

5.  download and install python
       https://www.python.org/downloads/
       https://www.python.org/ftp/python/2.7.14/python-2.7.14.msi

6. open command prompt (cmd) and enter:
      set http_proxy=localhost:5865

7. use pip to download and install plugins
    -cx_Oracle-doc
    pip --proxy http://127.0.0.1:5865 install cx_Oracle --upgrade
    -Jinja2-2.7.2
    pip --proxy http://127.0.0.1:5865 install Jinja2 --upgrade
    -mechanize-0.2.5
    pip --proxy http://127.0.0.1:5865 install mechanize --upgrade
    -beautifulsoup4
    pip --proxy http://127.0.0.1:5865 install beautifulsoup4 --upgrade

Windows 10 God Mode Folder on Desktop

If you have created a "God Mode" folder on your desktop - think twice before deciding to keep it there.
It turns out that some applications will refuse to perform their basic functionalities (like: open file, browse etc) and will crash - only because the folder sits on your desktop.


As always, had to learn that the hard way....

*If you don't know what the "God Mode" folder is - then probably you don't have one . This folder  usually is created explicitly by the user using some undocumented features of the Windows OS.




Windows 10 missing sticky notes and microsoft store problem

After installing Windows 10 I realized that the Microsoft Store was missing also the sticky note app was not there either...

The solution came from power-shell.
I guess Microsoft has adopted linux-like approach in getting updates and missing packages.

So here what I did:

 1. Enter Windows Power-Shell (as Admin)
 2. Get-Appxpackage -Allusers > res.txt
 3. Add-AppxPackage -register "C:\Program Files\WindowsApps\[packageFullName]\AppxManifest.xml" -DisableDevelopmentMode

Here the res.txt file is needed to find missing package's full name.

[Microsoft.MicrosoftStickyNotes]
[Microsoft.WindowsStore]

We use that name to download and install the package (step 3).

Tuesday, April 4, 2017

Creating web services with NetBeans

Creating web services with NetBeans

Here I will provide a description of an (finally successful) attempt to create secure web service, using Net Beans and Apache Tomcat. However I will try to describe step by step all the problems that I encountered along the way, and how overcame this obstacles.
So let's begin.

All the pitfalls will be numbered and  marked with brackets [].

First we need to install NetBeans 8 with  Apache Tomcat, and right in the beginning we encounter a curious problem [1] - Tomcat won't start from inside of the Netbeans! Bummer! Not a good start....

[1] To overcome this first obstacle we should set "NO Proxy" in NetBeans Tools->Options->General settings and vuola Tomcat miraculously starts!! (must be  a bug or something...)

OK, so far so good (like in the joke: "I Intend To Live Forever... So Far So good!!")

Next, we add some parameters to tomcat environment, so that we can debug sent - received soap messages (or do you expect everyting to work properly from the 1st attempt?).
So we add at Netbeans ->Servers->  TomCat - > properties -> platform -> VM Options

-Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true
-Dcom.sun.xml.internal.ws.transport.http.HttpAdapter.dump=true

(
  by the way, for a client, there is another variation

 -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dum‌p=true
 -Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump=true

)

If you use standalone Tomcat then for vm-options go to setevn.bat that you create and put besides catalina.bat (tomcat's bin folder)

For example:

rem contents of the setevn.bat 
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_77
set JAVA_OPTS="-Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true"
"-Dcom.sun.xml.internal.ws.transport.http.HttpAdapter.dump=true"



catalina start -security  (run tomcat with security manager)

To create Tomcat that will run as a service - we have slightly different approach

(set system variable)
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_77 

Then in dos prompt: 

cd C:\apache-tomcat-8.5.6\bin
service.bat install tomcat8

tomcat8w.exe 

also in java tab / java options set (tight secrity policy): 
-Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true
-Dcom.sun.xml.internal.ws.transport.http.HttpAdapter.dump=true
-Djava.security.manager
-Djava.security.policy=C:\apache-tomcat-8.5.6\conf\catalina.policy

You think we have finished? Not at all!! 

There will be also settings for https, certificates in server.xml , database passwords etc  but all that in due time... for now lets just create an unsecured service.
(by the way I would enable security manager at the very last stage - as there is a load of setting that should be performed in order for the application  to continue to operate with security manager![7])
Also, when deploying the application with security manager, app's context.xml should be copied to tomcat's conf/catalina folder, and renamed to appname.xml - otherwise the application won't deploy [3] (don't ask me why...)

Server 

1. Create new Web Project
2. Import METRO 2.0 library
    (actually you only need webservices-rt.jar to be packaged with final .war file - other libraries are  
    already present in tomcat - and may even cause problems with conflicting loader paths[4])
3. Add xsds that describe your messages
4. Create jax-b xsd binding (right click on packge - new jax-b binding)
5. Java Binding xsd and java code will be generated
6. Make sure in binding xsd's you have (originals may still refer to qualified )
    attributeFormDefault="unqualified"
    elementFormDefault="unqualified" [5]
 
    So that in generated package-info.java there is no reference to  elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
    Otherwise otherwise extra tag (name space) will be added inside your messages sent by the client
    (ns2:XXXX) and server would not understand the message!!![5]

7. Right click on project-> new -> Web service
8. In design view add operation, as parameters and returning results use object created by the jax-b
    binding procedure

Client

  1. Create new project (java application)
  2. Import METRO 2.0 library  (also may be JAX-WS 2.2.6)
  3. Create a main class
  4. Right click on project-> New -> Web Service Client (give the wsdl url of the web service)
  5. Right click - Insert Code - Web service call (select which operation to call)
  6. Because the generated java code is imperfect a meta tag should be added [6]
      @XmlRootElement(name="XX")
       public static class XX {
       otherwise marshalig /unmarshaling will not work! 
  7.  client input data could be in xml that would be umarshaled and send as parameter of ws call.
  8.  the reply can also be marshaled (if needed)


The unsecured part is ready to be tested! Fingers crossed - hope it works!

Next goal is to secure our communication channel!

To that end, first we should create proper keystores for the client and server, also we need to create truststores for both of them, which will store public keys of entities that are trusted.

Generate server keystore

"c:/Program Files/Java/jdk1.8.0_77/bin/keytool.exe" -genkey -keyalg RSA -alias mytomcat -dname "CN=Name, OU=Unit, O=Org, L=PlanetEarth, S=MyCity, C=FU" -keystore c:/certs/mytomcatkeystore.jks -validity 36000 -storepass changeit -keypass changeit -ext san=ip:X.X.X.X,ip:127.0.0.1,dns:www.mycompany.com

Export certificate (public key)

"c:/Program Files/Java/jdk1.8.0_77/bin/keytool.exe" -export -keystore c:/certs/mytomcatkeystore.jks -alias mytomcat -file c:/certs/myserver.cer -storepass changeit

Import into client's trust store 

"c:/Program Files/Java/jdk1.8.0_77/bin/keytool.exe" -importcert -file e:/certs/myserver.cer -keystore c:/certs/myclinetTrustStore.jks -alias "ClientT" -storepass changeit

Same for the client,

Generate clinent keystore

"c:/Program Files/Java/jdk1.8.0_77/bin/keytool.exe" -genkey -keyalg RSA -alias ourclient -dname "CN=Name, OU=Unit, O=Org, L=PlanetEarth, S=MyCity, C=FU" -keystore c:/certs/ourclientkeystore.jks -validity 36000 -storepass changeit -keypass changeit

Export certificate (public key)

"c:/Program Files/Java/jdk1.8.0_77/bin/keytool.exe" -export -keystore c:/certs/ourclientkeystore.jks -alias ourclient  -file c:/certs/ourclinet.cer -storepass changeit

Import into server's trust store 

"c:/Program Files/Java/jdk1.8.0_77/bin/keytool.exe" -importcert -file e:/certs/ourclinet.cer -keystore c:/certs/mytomcattruststore.jks -alias "TheClient" -storepass changeit

but also

Generate client certificate for web browser

"c:/Program Files/Java/jdk1.8.0_77/bin/keytool.exe" -importkeystore -srckeystore  c:/certs/myclient.jks -destkeystore c:/certs/myclient1.p12 -srcstoretype JKS -deststoretype PKCS12 - srcstorepass changeit -deststorepass changeit -srcalias MyClient -destalias MyClient1 -srckeypass changeit -destkeypass changeit -noprompt

After creating a key store we should set up tom cat to use it.
So back to tomcat settings.
Go to the tomcat/conf/server.xml

enable 8443 connector:

 <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
              maxThreads="150" SSLEnabled="true"
 keystoreFile="conf/mytomcatkeystore.jks" clientAuth="true"  scheme="https" secure="true"
 truststoreFile="file:///c:/apache-tomcat-8.5.6/conf/mytomcattruststore.jks" truststorePass="changeit" SSLProtocol="TLSv1.2"
 allowUnsafeLegacyRenegotiation="true"
/>

and also comment out this line [8]

<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

so that it becomes

<!--<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />-->

also, optionally (if you use database)

edit catalina.properties file, and add to the end of it the following line

mydatabse.password=changeit

so that, in your application's context.xml you could refer to database connection password as

 <Resource name="jdbc/DB" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@X.X.x.x:1521:xxxx"
              username="myuser" password="${mydatabse.password}" />

And now server is ready. 

On the client side (say you have java client)

the following chunk of code should be ran before ws call

public static void init(){
       System.setProperty("javax.net.ssl.trustStore","c:\\myclinetTrustStore.jks");
       System.setProperty("javax.net.ssl.trustStorePassword","changeit"); 
       System.setProperty("https.protocols", "TLSv1.2");
       System.setProperty("javax.net.ssl.keyStore","c:\\myclient.jks"); 
       System.setProperty("javax.net.ssl.keyStorePassword","changeit"); 
       System.setProperty("javax.net.debug","ssl,record,keygen,handshake"); 
    }

Code generation with python

Here I will preset a small code generator I am using to generate java, html, jsp, xsd and other codes.

I am using Python27 and Jinja2-2.7.2 

My Files:  

InputData.txt

:This is field list
field1:field1 description
field2:field2 description
field3:field3 description
field4:field4 description
field5:field5 description
field6:field6 description
field7:field7 description

CodeGenerator.py

import jinja2
import codecs
templateLoader = jinja2.FileSystemLoader( searchpath="." )
templateEnv = jinja2.Environment( loader=templateLoader )

TEMPLATE_FILE = "CodeGenerator.jinja"
template = templateEnv.get_template( TEMPLATE_FILE )


COLUMNS       = [tuple(line.split(':')) for line in codecs.open( "InputData.txt", "r", "utf-8" )]

COLUMNS       = map(lambda s: (s[0],(s[0].strip().title(),s[1].strip())), COLUMNS)
#title() copy of the string in which first characters of all the words are capitalized.
#strip()  copy of the string, all chars have been stripped from the beginning and the end
#lambda s --> (field1,(Field1,field1 description))

#ignore the first line
COLUMNS.pop(0)

#add variables to work with
templateVars = { "table" : "MyTableName",
                 "description" : "A simple code generator",
                 "columns" : COLUMNS
               }

outputText = template.render( templateVars )
f = open('Generated.txt', 'w')
outputText = outputText.encode('utf-8')
f.write(outputText)
f.close()
print outputText

CodeGenerator.jinja

//------------------model------------------------
//Generated code for model 
import java.io.Serializable;
public class {{table|capitalize}}_Model implements Serializable{
{%for columnName,columnTitle in columns%}
private String {{columnName|lower}};{%endfor%}

{% for columnName,columnTitle in columns %}
public void set{{columnName|capitalize}}(String {{columnName|lower}}){ 
  this.{{columnName|lower}}={{columnName|lower}};
}
public String get{{columnName|capitalize}}(){ 
  return this.{{columnName|lower}};
}
{% endfor %}
public {{table|capitalize}}_Model({% for columnName,columnTitle in columns %}String {{columnName|lower}}{% if not loop.last %},{% endif %}{% endfor %}){
        {% for columnName,columnTitle in columns %}
this.{{columnName|lower}}={{columnName|lower}}; {% endfor %}
    }
public String toString(){

return {% for columnName,columnTitle in columns %}"{{columnName|lower}}:" + this.{{columnName|lower}}{% if not loop.last %}+{% endif %}{% endfor %};

}

}
//------------------model------------------------
//
//------------------jsp example-----------------
{% for columnName,columnTitle in columns %}
<s:label  cssStyle ="margin-top:5px;color:blue;" value="{{columnTitle[1]}} :" id="{{columnName}}_label"/>
<s:textfield label="{{columnTitle[1]}}" name="myBean.{{columnName}}" />
{% endfor %}
//------------------jsp example-----------------

Sunday, July 31, 2016

IReport issues with Java 8

So IReport does not work  properly with Java 8... and I had to find that out the hard way ;-)

Here I have some tips for those of you who still use IReport (3 to 5)


When running with java 8 IReport3 will not show 'library' window contents  - where you usually expect to find 'buttons' like totals, page number, current date etc. and as far as I remember IReport5 will not launch at all.
You can check which java version is used by the IReport if you go Help->About->System Properties. There you can find 'java.version' variable.

So for ireport3 the workaround is to use ireport.bat file which is located in the ireport3 root directory.
Just add to the bat file something like path="C:\Program Files\Java\jdk1.6.0_26"
so that proper java will be used to launch IReport.

For irepor 5 you can edit  ireport.conf file (e.g. iReport-5.6.0\etc\ireport.conf)
and add  jdkhome="C:\Program Files\Java\jdk1.6.0_26"   

Note that you should have java6 jdk also installed on your system for the above to work.

That's it for today... 

Monday, January 25, 2016

Thunderbird Ldap address autocomplete problem...

Recently I installed Thunderbird (email client) and hooked it up to Ldap.

I set up the LDAP server as follows:

Port(default)   :  389
Base DN          :  dc=organization, dc=com
Bind  DN         :  myuser@organization.com
Filter(default) :  (objectclass=*)


The Problem:

1. Whenever I tried  to compose a new mail - the auto-complete feature of the "To" field either wasn't working at all or brought limited number of names from the Ldap server - without bringing the name that I was looking for.

On the other hand within the address book everything worked fine (searching / finding etc).

Very strange behavior...

2. Another thing I noticed was, that whenever I tried to create local replica of LDAP catalog - download failed.

Solution:

Solution I found for those two problems was to change the default port to 3268 which is apparently port for searching in Global Catalog.

Tuesday, August 25, 2015

Is My Intranet Web Server Down?

O.K.
Without further delay -  here is a python script that will e-mail you in case a Http server you are monitoring is down (based on failed ping or failed http connection) :

http_ping.py

-------------------------------------------------------------------------------------------------------------------
import smtplib
from email.mime.text import MIMEText
import optparse, subprocess, os, sys
import re, time, datetime
from optparse import OptionParser
from os import stat
from os.path import abspath
from stat import ST_SIZE
import httplib
import sys

SLEEP_TIME = 60
email_from = ""
email_to = ""
smtp_server=""
ssl=False
smtp_user=None
smtp_password=None

def runProcess(exe):
    FNULL = open(os.devnull, 'w')
    p = subprocess.call(exe, stdout=FNULL, stderr=FNULL)

    if p == 0:
        return True
    else:
        return False

def sendAlert(state, host, debug,what):
    subject = ""
    statetext = ""

    if state == 0:
        statetext = "up"
    elif state == 1:
        statetext = "down"

    subject = "Host %s is %s" % (host, statetext)

    ts = time.time()
    f_ts = str(datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))

    text = "Host " + host + " went " + statetext + " at " + f_ts + " based on " + what

    msg = MIMEText(text)
    msg[ 'From' ] = email_from
    msg[ 'To' ] = ", " + email_to
    msg[ 'Subject' ] = subject
    try:
       
        #if ssl/tls smtp
        if ssl:
            s = smtplib.SMTP_SSL(smtp_server)
            if debug: print "SSL SERVER:" +  smtp_server
        #if not encrypted
        else:
            s = smtplib.SMTP(smtp_server)
            if debug: print "PLAIN SMTP SERVER:" +  smtp_server
        if debug:
            s.set_debuglevel(True)
        #if authentication required
        if smtp_user!=None and smtp_password!=None:
            if debug: print "user and pass are set"
            s.login(smtp_user,smtp_password)
        s.sendmail(email_from, [email_to], msg.as_string())
        s.quit()
    except Exception as e:
        if debug: print "error sending email:"
        #if debug: print msg
        if debug: print e

def main():
    usage = "usage: %prog [options] arg"
    parser = OptionParser(usage)
    parser.add_option('-d', '--debug', action='store_true', dest='debug', default=False, help='enable debugging')
    parser.add_option('-e', '--encrypted', action='store_true', dest='ssl', default=False, help='ssl/tls smtp')
    parser.add_option('-p', '--ping', action='store', dest='host', default=None, help='specify host to ping')
    parser.add_option('-s', '--smtp', action='store', dest='smtp', default=None, help='Specify Mail Server (SMTP)')
    parser.add_option('-t', '--to', action='store', dest='mail_to', default=None, help='Specify Mail Receiver addresses')
   
   
    parser.add_option('-f', '--from', action='store', dest='mail_from', default='Http Ping', help='Specify Mail Sender address')
   
    parser.add_option('-u', '--smtp_user', action='store', dest='smtp_user', default=None, help='SMTP user (optional)')
    parser.add_option('-w', '--smtp_password', action='store', dest='smtp_password', default=None, help='SMTP password (optional)')
    parser.add_option('-z', '--sleepTime', action='store', dest='sleep', default=60, help='sleep time(optional)')
   

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit( 1 )
    (options, args) = parser.parse_args()

    if options.host == None:
        parser.print_help()
        sys.exit( 1 )

    if options.smtp == None:
        parser.print_help()
        sys.exit( 1 )

    if options.mail_to == None:
        parser.print_help()
        sys.exit( 1 )
    global  SLEEP_TIME,email_from,email_to,ssl,smtp_server
    SLEEP_TIME = int(options.sleep)
    email_from = options.mail_from
    email_to   = options.mail_to
    smtp_server=options.smtp
    ssl=options.ssl
   
   
    try:
        upflag = True
        while(True):
            if options.debug: print "pinging host: " + str(options.host)
            pingresult = runProcess(["ping", options.host])
            http_ping_result = False
            try:
                conn = httplib.HTTPConnection(options.host)
                conn.request('HEAD', '/')
                url = 'http://{0}/{1}'.format(options.host,"")
                if options.debug: print '    Trying: {0}'.format(url)
                response = conn.getresponse()
                if options.debug: print '    Got: ', response.status, response.reason
                conn.close()
                if response.status == 200:
                    if options.debug: print ("Got Response 200 " +" everything is ok")
                    http_ping_result = True   
            except:
                e = sys.exc_info()[0]
                print ' Problem in connection  ' + str(e)   
                http_ping_result = False
           
            if pingresult == False or http_ping_result==False:
                what=""
                if pingresult == False: what =what + "ping "           
                if http_ping_result == False: what =what + " http"           
                if options.debug: print "ping failed" + str(pingresult)
                if upflag == True:
                    if options.debug: print "send down email"
                    sendAlert(1,options.host,options.debug,what)
                   
                upflag = False
            else:
                if options.debug: print "ping was successful..."
                if upflag == False:
                    if options.debug: print "send up email"
                    sendAlert(0,options.host,options.debug,what)
                upflag = True
           
            if options.debug: print "sleeping for "+str(SLEEP_TIME) +"s"
            time.sleep(SLEEP_TIME)
    except (KeyboardInterrupt, SystemExit):
        #raise
        print "Exiting"
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)
        exit ( 1 )
    exit( 1 )

if __name__ == '__main__':
    main()
---------------------------------------------------------------------------------------------------------------------
usage example: 

http_ping.py -d -p my.Server.com -s my.mail.server.com -t my.mail@my.mail.com -z 30

Sunday, July 26, 2015

How to fix windows 7 weather gadget

If you were using weather gadget on windows 7, then probably (to your disappointment) you discovered one day that after some security updates - it stopped working. What a loss! :(

Luckily, it turns out that fixing the problem is not so hard after all.
All you need to do is to change modification date of a config.xml file used by the gadget.

So open your notepad as administrator (right click->run as admin) and navigate to the following path:

C:\Users\[UserName]\AppData\Local\Microsoft\Windows Live\Services\Cache 

and open  config.xml file located there.

*make sure that you replace [UserName] with the user name you are using on your machine.

Now to change the modification date you just tamper a little with your pc's clock and set it say to the year 2019, after that you 'modify' the config.xml (just type space character somewhere and then delete it) and save the changes.
That's it - your file modification date is 2019 way ahead in the future ... and the widget is working again!!

Don't forget to set your clock to correct time again.

Monday, February 2, 2015

 How to Stop Chrome Running in the Background

Well it has been a while since my last post. This time I just want to tell you how to get rid off that Chrome process which is always running in the background eating up your PC's resources. Actually it is quite simple. 

1. Open your chrome browser 

2. Select Settings 

3. Click on the link titled ‘Show advanced settings‘

4. Under the section headed ‘System‘ untick the box next to “Continue running background apps  when Google Chrome is closed”

 

Saturday, November 16, 2013

Glassfish hangs on leak reclaim


OK, let me tell you a painful story about a developer an application and an application server called Glassfish 3.1.2.2 ;-) Beware, this story is still in progress so I may still be updating this post till the issue is resolved.
(Forgive me my poetic mood - this post will be less technical than the previous ones ..though you will find some technical details, solutions etc in this text .. somewhere ;-)

So what the problem was/is? At the beginning it appeared to be a total mystery. After running cheerfully on the server for days or even weeks...application hangs ...and nothing could be done to bring it back to life, except for restarting the whole server (I mean the machine itself) - and we are talking about production environment here!  
Something had be done ..but what ? Fortunately, there is this monitoring tool  that came to my attention  - VisualVM and it has glassfish plugin. Installation of this product was definitely first step in the right direction.
The tool revealed that during the freeze or  'deadlock' all http thread-pool threads were waiting to lock Object which was held by "connector-timer-proxy".  Connector-timer-proxy itself was trying to reclaim a connection that was marked as 'leaked' but for some reason couldn't reclaim it. Taking a look at database connections revealed 4 inactive connections....and one active (the one that should have been reclaimed).
Killing the database connection - brought  Glassfish back to life ... everything unfreezed!
My thoughts on this one are that the connection was actually doing something (admittedly it must have been a very long operation) and thus the connection couldn't be reclaimed. Or I am wrong here? I don't know yet.
One thing I did, I set statement timeout on Glassfish. So I suppose if for some reason I have a statement running for too long ... it will be canceled first and then  if need the connection could be reclaimed without problem... Let's hope it is the case, and not the fact that I am dealing with some kind of GF's bug...(Time will show).
Anyway, if you have any ideas on this matter - don't be shy - let me know...

Sunday, July 14, 2013

Android Emulator too slow

Wanted for a while now to try out android programming without actually owning android device ... tried once with emulator which comes with android sdk and eclipse plugins  and was deeply disappointed ... too damn slow.

Fortunately there are solutions out there that are much  faster - e.g.  android-x86 that can be installed on virtual box or the VMware. Although bare in mind that some versions do not have out-of-the-box support for Internet or Sound (it takes some intervention to make these features work).

I personally use VMware (seems faster than vBox) and luckily I found an android-x86 v4.0 with Ethernet and sound enabled. If you are interested you can download the image here  http://www.buildroid.org/Download/android-x86-vm-20120307.iso.gz.

If you need instructions on how to install anroid-x86 on virtual machines just search the web as I did.

Happy android programming ! ;-)


10/01/2014 just an update.
Some useful tips if you are using Android on VMware.

1 .To find out the IP address of your virtual android device just enter terminal emulator and type:
     "netcfg"

2. To connect eclipse to Android on VM:
    a. enter command prompt on your PC: type cmd in start menu
    b. go to the directory  where android platform tools are installed
    (e.g. cd "C:\Program Files\Android\android-sdk\platform-tools")
    c. Then type adb connect <IP Address of the Android on VM>

Wednesday, May 15, 2013

Firefox new pdf viewer (pdf.js) print problem

If you are using pdf to generate reports, than probably your users have some problems with newer versions of firefox (starting with v19). A new build in viewer doesn't handle printing quite well.
I'll suggest you to switch back to Adobe Acrobat instead till the problem is fixed.

To do that just:
  1. Select Options
  2. Select the Applications panel.
  3. Find Portable Document Format (PDF) in the list.
  4. Click on the drop-down arrow in the Action column for the above entry and select the use Adobe Acrobat (in Firefox) .

    PDFviewer&plugin-Fx19
     
  5. Click OK to close the Options window

Sunday, April 14, 2013

Struts2 + Hibernate + Jpa 2 + Spring + Netbeans how to make it all work together

After many days of frustration, searching, trying ..and failing finally got it working and because I couldn't locate any full descriptions online I will lay out my solution here.

Bare in mind that if you use different versions of these libraries, you should be sure that they are compatible with each other, because one of the issues I was dealing with (and I didn't know that at the time) was because of incompatible libraries.

So I am using:

netbeans  7.3

hibernate: hibernate-release-4.1.8.Final download
                  (+ hibernate-entitymanager-4.0.1.Final) download


listing:
hibernate-release-4.1.8.Final/lib/required/antlr-2.7.7.jar
hibernate-release-4.1.8.Final/lib/required/dom4j-1.6.1.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-commons-annotations-4.0.1.Final.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-core-4.1.8.Final.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-release-4.1.8.Final/lib/required/javassist-3.15.0-GA.jar
hibernate-release-4.1.8.Final/lib/required/jboss-logging-3.1.0.GA.jar
hibernate-release-4.1.8.Final/lib/required/jboss-transaction-api_1.1_spec-1.0.0.Final.jar
hibernate-entitymanager-4.0.1.Final


aopalliance: aopaliance 1.0  download 

aopalliance.jar

Spring    : spring3.1.1release (bundled with netbeans  7.3)


listing: 

spring-aop-3.1.1.RELEASE.jar
spring-asm-3.1.1.RELEASE.jar
spring-aspects-3.1.1.RELEASE.jar
spring-beans-3.1.1.RELEASE.jar
spring-context-3.1.1.RELEASE.jar
spring-context-support-3.1.1.RELEASE.jar
spring-core-3.1.1.RELEASE.jar
spring-expression-3.1.1.RELEASE.jar
spring-instrument-3.1.1.RELEASE.jar
spring-instrument-tomcat-3.1.1.RELEASE.jar
spring-jdbc-3.1.1.RELEASE.jar
spring-jms-3.1.1.RELEASE.jar
spring-orm-3.1.1.RELEASE.jar
spring-oxm-3.1.1.RELEASE.jar
spring-struts-3.1.1.RELEASE.jar
spring-test-3.1.1.RELEASE.jar
spring-tx-3.1.1.RELEASE.jar
spring-web-3.1.1.RELEASE.jar
spring-webmvc-3.1.1.RELEASE.jar
spring-webmvc-portlet-3.1.1.RELEASE.jar



Struts2    : struts-2.3.12 download
listing:
(remember to remove unnecessary plugins - otherwise you will have problems deploying e.g I am using tiles, so I have left the plugin in listing but I also have the corresponding  tiles.xml to make it work, if you don't just do not include struts2-tiles-plugin)

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
classworlds-1.1.jar
commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
commons-validator-1.3.1.jar
ezmorph-1.0.6.jar
freemarker-2.3.19.jar
google-collections-1.0.jar
google-gxp-0.2.4-beta.jar
jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
javassist-3.11.0.GA.jar
json-lib-2.3-jdk15.jar
juli-6.0.18.jar
ognl-3.0.6.jar
org.apache.felix.bundlerepository-1.2.1.jar
org.apache.felix.framework-1.4.1.jar
org.apache.felix.main-1.4.1.jar
org.apache.felix.shell-1.0.2.jar
org.apache.felix.shell.tui-1.0.2.jar
oval-1.31.jar
plexus-container-default-1.0-alpha-10.jar
plexus-utils-1.2.jar
struts2-convention-plugin-2.3.12.jar
struts2-core-2.3.12.jar
struts2-dojo-plugin-2.3.12.jar
struts2-embeddedjsp-plugin-2.3.12.jar
struts2-jasperreports-plugin-2.3.12.jar
struts2-javatemplates-plugin-2.3.12.jar
struts2-spring-plugin-2.3.12.jar
struts2-tiles-plugin-2.3.12.jar
testng-5.1-jdk15.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
xmlpull-1.1.3.1.jar
xpp3_min-1.1.4c.jar
xstream-1.4.2.jar
xwork-core-2.3.12.jar




web.xml
...
 <filter>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  <!-- deprecated
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>my.action</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

...

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:aop="http://www.springframework.org/schema/aop"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:p="http://www.springframework.org/schema/p" 
               xmlns:cache="http://www.springframework.org/schema/cache" 
               xmlns:jee="http://www.springframework.org/schema/jee"
               xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                    http://www.springframework.org/schema/context     
                    http://www.springframework.org/schema/context/spring-context-3.1.xsd
                    http://www.springframework.org/schema/cache
                    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
                    http://www.springframework.org/schema/jee
                    classpath:org/springframework/ejb/config/spring-jee-3.1.xsd"> 
     
      <context:component-scan base-package="myBasePackage" />

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/DATABASENAME"/>
    </bean>
   
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
                   <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="database" value="ORACLE"/>
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
        </bean>
    </property>
      </bean>

      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
           <property name="entityManagerFactory" ref="entityManagerFactory" /> 
      </bean> 
      <!--mybeans -->
      <bean id="daoExample" class="my.dao.DaoExample"/>
      <bean id="portfolioService" class="my.utilities.ServiceTest"/>
      <tx:annotation-driven transaction-manager="transactionManager"/>
     
</beans>



Thursday, April 4, 2013

Netbeans Maven Proxy Settings

You can set up proxy for maven project in Netbeans 7.3 (on Windows)  by editing


C:\Program Files\NetBeans 7.3\java\maven\conf\settings.xml.

That simple... It took me 30 min to find it on net  - hope you found this page much quicker ;-)

Barbecue barcode label problem and solution

I am using barbecue 1.5.beta to generate barcode images on my Windows 2003 server.
When migrating to a new server though (Windows 2008 x64 ) I encountered the following issue: text label underneath the barcode wasn't showing any more...users where baffled by this ..and so was I .

After searching the web I came to a conclusion that this must be happening because some servers (don't ask me why) are considered to be headless (without monitor) and in that case...well there is a bug in Barcode class namely in its calculateSize() method.  Credits for this solution must go to Paul

So, to fix Barcode.class you must replace its calculateSize() method, as Paul suggested:

-------------------------------------

private Dimension calculateSize() { 
Dimension d = new Dimension(); 
   if(EnvironmentFactory.getEnvironment() instanceof HeadlessEnvironment)
     try { 
          if(font == null) { 
               d = draw(new SizingOutput(font, getForeground(), getBackground()), 0, 0, barWidth, barHeight);
         } 
         else { 
              java.awt.FontMetrics fontMetrics = getFontMetrics(font); 
              d = draw(new SizingOutput(font, fontMetrics, getForeground(), getBackground()), 0, 0, barWidth, barHeight); 
              }
         } 
      catch(OutputException e) { 
          e.printStackTrace(); 
      } 
   else 
    try { 
         java.awt.FontMetrics fontMetrics = null;
         if(font != null) fontMetrics = getFontMetrics(font);
         d = draw(new SizingOutput(font, fontMetrics, getForeground(), getBackground()), 0, 0, barWidth, barHeight);
    } catch(OutputException e) { }
 return d; 
}

-------------------------------------
Works like charm.  If you have enough patience you should download Barbecue source code, downland dependencies,  use maven,  make changes ...recompile (as I did). Or if you don't -  then just take  this  Barbecue (myFix) version.