Saturday, October 29, 2016

Programming in Scala 7/8/9/10

1.

scala> val someNumbers = List(-11,2,-12,8,9)
scala> someNumbers.filter( x=>x<8 )
scala> someNumbers.filter(_>0)
scala> val f= (_: Int)+(_: Int)
scala> someNumbers.foreach(println _)
 equals
 someNumbers.foreach(x => println(x))
2. closure

 def makeIncreaser(more: Int) = (x: Int) => x + more
3.
 scala> def twice(op: Double => Double, x: Double) = op(op(x))
 scala> twice(_ + 1, 5)
4.
In Scala it is forbidden to define a field and method with the same name in the same class, whereas this is
allowed in Java.
Generally, Scala has just two namespaces for definitions in place of Java's four. Java's four namespaces
are fields, methods, types, and packages. By contrast, Scala's two namespaces are:
• values (fields, methods, packages, and singleton objects)
• types (class and trait names)

Tuesday, October 25, 2016

Programming in Scala Note 4/5/6

1.

The way you make members public in Scala is by not explicitly specifying any access modifier.
Public is Scala's default access level.
2.
one way in which Scala is more object-oriented than Java is that classes in
Scala cannot have static members. Instead, Scala has singleton objects. A singleton object definition
looks like a class definition, except instead of the keyword class you use the keyword object.
A singleton object is more than a holder of static methods, however. It is a first-class object.
One difference between classes and singleton objects is that singleton objects cannot take parameters,
whereas classes can. Because you can't instantiate a singleton object with the newkeyword, you have
no way to pass parameters to it.
3.
in Scala, you
can name .scala files anything you want, no matter what Scala classes or code you put in them.
4. ==First check the left side for null. If it is not null,

call the equals method.
scala> 1==1.0

res8: Boolean = true
5. 
In Java, classes have
constructors, which can take parameters; whereas in Scala, classes can take parameters directly. The

Scala notation is more concise—class parameters can be used directly in the body of the class
6.
 class Rational(n: Int, d: Int) {
require(d != 0)
override def toString = n + "/" + d
}
7. 
In Scala, every auxiliary constructor must invoke another constructor of the same class as its first
action. In a Scala class, only the primary
constructor can invoke a superclass constructor.
8.
 scala> implicit def intToRational(x: Int) = new Rational(x)

Programming in Scala Note Chapter 1/2/3

Chapter 1/2/3
1.

def factorial(x: BigInt): BigInt =
    if (x == 0) 1 else x * factorial(x - 1)
2.
A val is similar to a final variable in Java. Once
initialized, a val can never be reassigned. A var, by contrast, is similar to a non-final variable in Java. 
3.
Sometimes the Scala compiler will require you to specify the result type of a function. If the function
is recursive,[7] for example, you must explicitly specify the function's result type.
If a function consists of just one statement, you can optionally leave off the curly braces.
scala> def max(x: Int, y: Int) = if (x > y) x else y
4.
scala> def greet() = println("Hello, world!")
greet: ()Unit
A result type of Unit indicates the function returns no interesting value.
5.
Note that Java's + +i and i++ don't work in Scala. To increment in Scala, you need to say either i = i + 1 or i += 1.
6.
args.foreach(arg => println(arg))
or   
args.foreach((arg: String) => println(arg))
or
args.foreach(println)
or
for(arg <- args) println(arg)
7.
If the method name ends in a colon, the
method is invoked on the right operand.
8.
The time it takes to append to a list grows linearly with the size of the
list, whereas prepending with :: takes constant time. If you want to build a list efficiently by appending
elements, you can prepend them and when you're done call reverse. Or you can use a ListBuffer, a
mutable list that does offer an append operation, and when you're done call toList.
9.
Once you have a tuple instantiated, you can access its elements individually with a dot,
underscore, and the one-based index of the element.
val pair = (99, "Luftballons")
println(pair._1)
10.
This -> method, which you can invoke on any
object in a Scala program, returns a two-element tuple containing the key and value.
import scala.collection.mutable
val treasureMap = mutable.Map[Int, String]()
treasureMap += (1 -> "Go to island.")
11.
if code contains any vars, it is probably in an imperative style. If the code contains no vars at all—i.e., it
contains only vals—it is probably in a functional style.

Friday, September 30, 2016

My SOAP based web service client development and test note

My SOAP based web service client development and test note:
1. If wsdl is available, SOAP UI is a good tool for the test. If there's error, then use local wsdl file and it could be fixed
2. If SOAP request file is available (you can get it from the server side or SOAP UI), then Apache Common http client is good to post the SOAP request
    to the end point directly:
String strURL = "https://...";
String strSoapAction = "retrieve";
File input = new File(strXMLFilename);
PostMethod post = new PostMethod(strURL);
RequestEntity entity = new FileRequestEntity(input,
"text/xml; charset=UTF-8");
post.setRequestEntity(entity);
post.setRequestHeader("SOAPAction", strSoapAction);
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
...
Still, the response could be parsed by JAXB like (Use the regular expression "<soapenv:Body>(.*?)</soapenv:Body>" to get the body part retrieveResponseStr):
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(retrieveResponseStr);
RetrieveResponse retrieveResponse = ((JAXBElement<RetrieveResponse>) unmarshaller
.unmarshal(reader)).getValue();


3. Or SAAJ could be used to construct the SOAP message as this link shows
http://stackoverflow.com/questions/15940234/how-to-do-a-soap-web-service-call-from-java-class
4. The most common way is to generate the client by invoking "wsimport -keep ...", and then
   If need customize SOAP header, then implement javax.xml.ws.handler.soap.SOAPHandler, call service's method setHandlerResolver before getPort
   Be careful not to introduce WAS_V7JAXWS_WEBSERVICES_THINCLIENT if you use IBM RAD. Or you may get error "WSWS7130E: No Secure Sockets Layer (SSL) configuration is available"
   or "IOException: Connection close: Read failed.  Possible end of stream encountered".

TCP/IP monitor in Eclipse cannot support https monitor
I cannot enable Fiddler to monitor Java web service client for https

A weird java web service client error

When I developed web service client in Rational Application Developer, I got the below error:
Exception in thread "main" javax.xml.ws.WebServiceException: org.apache.axis2.AxisFault: WSWS7130E: No Secure Sockets Layer (SSL) configuration is available for the https://... endpoint.

After searching the google, got a useful article "Using Secure Web Services HTTPs (SSL)":
http://btarlton.blogspot.ca/2013/03/ever-have-trouble-using-ssl-to-call-web.html
So I exported the certificate and add the below to VM arguments:
-Djavax.net.ssl.trustStore=C:\temp\myTrustStore -Djavax.net.ssl.trustStorePassword=changeit
Then the error "No Secure Sockets Layer ... " is gone.
However, another error is raised: "java.io.IOException: Connection close: Read failed.  Possible end of stream encountered. "

I searched a lot and tried some ways like setting com.ibm.websphere.webservices.http.requestResendEnabled but still got the error.

Finally I decided to regenerate the client from scratch using wsimport -keep, and copy them into RAD7.5, Phew, everything works perfect.

So what's the problem? I compared the two projects source code until they are all identical through WinMerge. But their behavior are still different.
Then I checked the projects setting in RAD and removed the auto introduced WAS_V7JAXWS_WEBSERVICES_THINCLIENT, now both of them works like a charm.

It's so misleading and confusing for the IDE especially if it involves IBM product. Under such case, should remove the dependency on IDE as much as possible
so that we can isolate the issue.

Sunday, September 25, 2016

My Plan

1. Sleep before/at 11:00 and get up at 6:30
2. Play basketball at Monday & Wednesday evening, 8:15 - 09:45
    swim at Saturday morning 7:00-8:30
3. Push-up and other physical exercise at Tuesday, Thursday and Saturday evening
Runtastic 6 packs and push-up at Tuesday and Thursday
4. Improve English
a. Watch TV more and surf the web less
b. Read English news instead of Chinese news
c. Jerry English 

Saturday, June 18, 2016

Notepad++ replace with regular expression

(\w+),$
logger.info\("\1="+\1\);

aomBeforeChildItemCode,
aomByitmcat,

to

logger.info("aomBeforeChildItemCode="+aomBeforeChildItemCode);
logger.info("aomByitmcat="+aomByitmcat);