tag:blogger.com,1999:blog-40378372140266306842024-03-13T18:42:02.552+02:00Tzach Solomon's blogAnonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-4037837214026630684.post-70013330021111029932016-06-20T11:13:00.002+03:002016-06-20T11:15:18.513+03:00Invoking Jenkins job with File Parameter using HTTPI came across this great answer from Jayan on how to invoke a Jenkins job with File parameter so I had to share (and bookmark it on the way :)<br />
<br />
<a href="http://stackoverflow.com/questions/34517388/how-to-pass-file-to-jenkins-with-java">http://stackoverflow.com/questions/34517388/how-to-pass-file-to-jenkins-with-java</a>Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0tag:blogger.com,1999:blog-4037837214026630684.post-27047226897745693482016-06-12T18:57:00.000+03:002016-06-12T18:57:22.016+03:006 tips to improve your exception handling by Dele Taylor - all credI saw this great article about exception handling, I've had to put a link to it from here also :)<br />
<br />
<a href="https://northconcepts.com/blog/2013/01/18/6-tips-to-improve-your-exception-handling/">https://northconcepts.com/blog/2013/01/18/6-tips-to-improve-your-exception-handling/</a><br />
<br />
All credits goes to Dele TaylorAnonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0tag:blogger.com,1999:blog-4037837214026630684.post-46558204066231712502016-05-19T14:50:00.002+03:002016-05-26T19:44:35.568+03:00Invoking Jenkins Job using Jersey Client in Java with Username / Password authentication<h2>
General</h2>
<div>
The following post is document how to invoke a job using Jersery client in Java on Jenkins</div>
<div>
<br /></div>
<h2>
Assumptions</h2>
<div>
<ul>
<li>You are familiar with Jenkins and have permissions to create a new job</li>
<li>I've used Jenkins version is 1.651.1 ( I'm guessing it will work on previous versions but I did not check)</li>
<li>You are familiar with Maven</li>
<li>You have IntelliJ ( I used Community Edition 2016.1)</li>
</ul>
<h2>
Setup</h2>
</div>
<h3>
Jenkins</h3>
<h4>
Creating the job</h4>
<div>
<ul>
<li>Connect your Jenknis</li>
<li>Click on New Item</li>
<li>In Item name type 'hello_world'</li>
<li>Select 'Freestyle project'</li>
<li>Press ok</li>
</ul>
<h4>
Configuring the job</h4>
</div>
<div>
<ul>
<li>Click on the job</li>
<li>Press on the configure link from the left</li>
<li>Check the 'This build is parameterized'</li>
<li>Click on 'Add Parameter'</li>
<li>Choose String Parameter</li>
<li>In the name type 'firstname' (Leave the Default value and description empty)</li>
<li>Scroll down to the Build section</li>
<li>Click on 'Add build step' and choose Execute shell</li>
<li>type 'echo ${firstname}'</li>
</ul>
</div>
<h4>
Test the job</h4>
<div>
<ul>
<li>Click on the 'Build with Parameters'</li>
<li>Fill in a name click on the build number (should be #1) in case this job is new</li>
<li>Click on Console Output</li>
<li>You should see something like this</li>
</ul>
<div>
<div>
+ echo checking</div>
<div>
checking</div>
</div>
<div>
<br /></div>
<h3>
Java</h3>
<h4>
Creating the project</h4>
<div>
<ul>
<li>Open IntelliJ</li>
<li>Click on 'Create New Project'</li>
<li>Choose maven and click next</li>
<li>GroupId: com.tzach.jenkins</li>
<li>ArtifactId: jerseyclient</li>
<li>Project name: jersey_client_jenkins</li>
<li>Open the pom.xml file</li>
<li>Add the following</li>
</ul>
<div>
<div>
<div>
<dependencies></div>
<div>
<dependency></div>
<div>
<groupId>org.glassfish.jersey.core</groupId></div>
<div>
<artifactId>jersey-client</artifactId></div>
<div>
<version>2.22.2</version></div>
<div>
</dependency></div>
<div>
</dependencies></div>
</div>
</div>
</div>
</div>
<div>
<br /></div>
<div>
<ul>
<li>Click on Enable auto import</li>
<li>Click on Maven from the right and choose compile</li>
<li>On the bottom you should see 'Process finished with exit code 0'</li>
</ul>
<h2>
Now let's write some code</h2>
</div>
<div>
you can see the class @ <a href="https://github.com/tzachs/JenkinsJerseyClient/">https://github.com/tzachs/JenkinsJerseyClient/</a></div>
<div>
<ul>
<li>Create a new class named JenkinsJerseyClient</li>
<li>Create the following method: Response invoke(String username, String password, String firstname)</li>
<li>Paste the following</li>
</ul>
<div>
<div>
String target = "http://myjenkins:8080/job/hello_world/buildWithParameters";</div>
<div>
<br /></div>
<div>
HttpAuthenticationFeature httpAuthenticationFeature = HttpAuthenticationFeature.basicBuilder()</div>
<div>
.credentials(username, password)</div>
<div>
.build();</div>
<div>
<br /></div>
<div>
ClientConfig clientConfig = new ClientConfig();</div>
<div>
clientConfig.register(httpAuthenticationFeature);</div>
<div>
<br /></div>
<div>
Client client = ClientBuilder.newClient(clientConfig);</div>
<div>
<br /></div>
<div>
WebTarget webTarget = client.target(target);</div>
<div>
// Parameters needed for the job</div>
<div>
Form form = new Form();</div>
<div>
form.param("firstname", firstname);</div>
<div>
<br /></div>
<div>
// Sending invoke as POST</div>
<div>
return webTarget.request(MediaType.APPLICATION_JSON_TYPE)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));</div>
</div>
<div>
<br /></div>
<div>
<ul>
<li>Replace the URL of jenkins to your jenkins</li>
<li>Create a main method, and paste the following:</li>
</ul>
<div>
System.out.println(new JenkinsJerseyClient().invoke("tzachs","tzachs_password","tzach").getStatus());</div>
</div>
<div>
<br /></div>
<div>
<ul>
<li>Replace 'tzachs' with your username</li>
<li>Replace 'tzachs_password' with your password</li>
<li>Replace 'tzach' with the parameters you need</li>
<li>Full code looks like this:</li>
</ul>
<div>
<br /></div>
</div>
<div>
<div>
import org.glassfish.jersey.client.ClientConfig;</div>
<div>
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;</div>
<div>
<br /></div>
<div>
import javax.ws.rs.client.Client;</div>
<div>
import javax.ws.rs.client.ClientBuilder;</div>
<div>
import javax.ws.rs.client.Entity;</div>
<div>
import javax.ws.rs.client.WebTarget;</div>
<div>
import javax.ws.rs.core.Form;</div>
<div>
import javax.ws.rs.core.MediaType;</div>
<div>
import javax.ws.rs.core.Response;</div>
<div>
<br /></div>
<div>
/**</div>
<div>
* Created by tzachs on 5/19/2016.</div>
<div>
*/</div>
<div>
public class JenkinsJerseyClient {</div>
<div>
<br /></div>
<div>
public Response invoke(String username, String password, String firstname){</div>
<div>
String target = "http://myjenkins:8080/job/hello_world/buildWithParameters";</div>
<div>
<br /></div>
<div>
HttpAuthenticationFeature httpAuthenticationFeature = HttpAuthenticationFeature.basicBuilder()</div>
<div>
.credentials(username, password)</div>
<div>
.build();</div>
<div>
<br /></div>
<div>
ClientConfig clientConfig = new ClientConfig();</div>
<div>
clientConfig.register(httpAuthenticationFeature);</div>
<div>
<br /></div>
<div>
Client client = ClientBuilder.newClient(clientConfig);</div>
<div>
<br /></div>
<div>
WebTarget webTarget = client.target(target);</div>
<div>
Form form = new Form();</div>
<div>
form.param("firstname", firstname);</div>
<div>
<br /></div>
<div>
return webTarget.request(MediaType.APPLICATION_JSON_TYPE)</div>
<div>
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static void main(String[] args) {</div>
<div>
System.out.println(new JenkinsJerseyClient().invoke("tzachs","tzachs_password","tzach").getStatus());</div>
<div>
}</div>
<div>
}</div>
</div>
<div>
<br /></div>
<h4>
Run</h4>
<div>
<ul>
<li>Run the main, you should see in the console log 201. If you see it, it's working. Go to jeknins see you have a new build number</li>
</ul>
</div>
<div>
<br /></div>
</div>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0tag:blogger.com,1999:blog-4037837214026630684.post-26865424521695271242016-04-28T09:33:00.001+03:002016-04-28T09:33:14.680+03:00Connecting IntelliJ and JIRA with SSL (java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty)<h2>
Connecting IntelliJ and JIRA with SSL (java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty)</h2>
<div>
My work place change the JIRA connection from HTTP to HTTPS meaning now JIRA was working with SSL. </div>
<div>
Everything was working fine with Chrome but when trying to work with IntelliJ and JIRA using the Atlassian plugin (<a href="https://confluence.atlassian.com/display/IDEPLUGIN/Installing+the+IntelliJ+Connector">https://confluence.atlassian.com/display/IDEPLUGIN/Installing+the+IntelliJ+Connector</a>) I've encountered the following error: </div>
<div>
<br /></div>
<div>
<a href="http://2.bp.blogspot.com/-c6PTHXulORo/VyGnZ6DLVwI/AAAAAAAA_2c/jx6BBbIAvjAJLYQC5Boul3fiUa95Im_rgCK4B/s1600/2016-04-28%2B09_01_20-Atlassian%2BConnector%2Bfor%2BIntelliJ%2BIDEA.png" imageanchor="1"><img border="0" height="144" src="https://2.bp.blogspot.com/-c6PTHXulORo/VyGnZ6DLVwI/AAAAAAAA_2c/jx6BBbIAvjAJLYQC5Boul3fiUa95Im_rgCK4B/s320/2016-04-28%2B09_01_20-Atlassian%2BConnector%2Bfor%2BIntelliJ%2BIDEA.png" width="320" /></a></div>
<div>
<br /></div>
<div>
Clicking on Show Details was not that helpful and displayed the following stack trace:</div>
<div>
<br /></div>
<div>
<div>
<i>com.atlassian.theplugin.commons.remoteapi.RemoteApiException: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at com.atlassian.connector.commons.jira.soap.JIRASoapSessionImpl.login(JIRASoapSessionImpl.java:193)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at com.atlassian.connector.commons.jira.JIRASoapAndXmlServerFacade2Impl.testServerConnection(JIRASoapAndXmlServerFacade2Impl.java:154)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at java.lang.reflect.Method.invoke(Method.java:497)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at com.atlassian.connector.commons.jira.JIRAServerFacade2Impl$1.invoke(JIRAServerFacade2Impl.java:93)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at com.sun.proxy.$Proxy41.testServerConnection(Unknown Source)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at com.atlassian.connector.commons.jira.JIRAServerFacade2Impl.testServerConnection(JIRAServerFacade2Impl.java:268)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at com.atlassian.theplugin.commons.jira.IntelliJJiraServerFacade.testServerConnection(IntelliJJiraServerFacade.java:473)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at com.atlassian.theplugin.idea.config.serverconfig.ProductConnector.connect(ProductConnector.java:35)</i></div>
<div>
<i><span class="Apple-tab-span" style="white-space: pre;"> </span>at com.atlassian.theplugin.ConnectionWrapper.run(ConnectionWrapper.java:63)</i></div>
</div>
<div>
<br /></div>
<h2>
The problem</h2>
<div>
Intellij now needed the Certificate JIRA was using in order to establish a connection</div>
<div>
<br /></div>
<h2>
How to solve?</h2>
<div>
Thanks to the IT guy at my work, I understood I needed to fetch the certificate and tell IntelliJ to use the key store in which I loaded the certificate to.</div>
<div>
<br /></div>
<div>
Here is how to do it.</div>
<div>
<ul>
<li>You have JRE / JDK installed</li>
<li>Make sure you have openssl.exe (I'm assuming you are on windows). If you don't have you can get it by installing openvpn (get it from here <a href="https://openvpn.net/">https://openvpn.net/</a>)</li>
<li>Open command line</li>
<li>Execute: openssl s_client -connect <JIRA_DNS_NAME>:443 where <JIRA_DNS_NAME> should be replaced with the your JIRA dns name / IP (for example openssl s_client -connect jira:443)</li>
<li>Copy the output the last command generated from ---BEGIN CERTIFICATE--- to ---END CERTIFICATE-- including those lines to a file named public.cert</li>
<li>Execute: keytool -import-alias <NAME_OF_ALIAS> -file public.cert. For example, keytool -import -alias jira -file public.cert</li>
<li>It will ask for a password. The default password is 'changeit'</li>
<li>Answer yes to 'Trust this certificate'</li>
<li>You should receive 'Certificate was added to keystore'</li>
<li>Verify the certificate was really added using the following command: keytool -list | findstr <NAME_OF_ALIAS>, for exampe keytool -list | findstr jira</li>
<li>You should see something like this: jira, Apr 28, 2016, trustedCertEntry,</li>
<li>Go to the installation of IntelliJ. Default installation in windows is at '<i>C:\Program Files (x86)\JetBrains\<INTELLIJ_PRODUCT>\bin</i>'</li>
<li>If you have multiple installation, notice which one you choose, in my case it was '<i>C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1.1\bin</i>'</li>
<li>Edit the file idea.exe.vmoptions (or idea64.exe.vmoptions, depends which one you are running)</li>
<li>Add the following: -Djavax.net.ssl.trustStore=c:\Users\<YOUR USERNAME>\.keystore, for example, in my case it was '<i>-Djavax.net.ssl.trustStore=c:\Users\tzachs\.keystore'</i></li>
<li>Restart IntelliJ</li>
</ul>
<h2>
<b>Troubleshoot</b></h2>
</div>
<div>
In case something does not work, check the idea.log. </div>
<div>
The idea.log depends on your OS so check here for more info - <a href="https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs">https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs</a></div>
<div>
<br /></div>
<h2>
For more reading</h2>
<div>
<br /></div>
<div>
<a href="https://confluence.atlassian.com/jira/connecting-to-ssl-services-117455.html">https://confluence.atlassian.com/jira/connecting-to-ssl-services-117455.html</a></div>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com1tag:blogger.com,1999:blog-4037837214026630684.post-5759536050683137472016-04-03T11:39:00.003+03:002016-04-03T11:40:06.715+03:00Java script not working in Jenkins due new Content security policy, chrome console log 'displays allow-scripts' permission is not set.<h2>
Problem</h2>
<div>
We run tests using our Jenkins CI and our report contains java script. From version 1.625 LTS we saw that our report is corrupted, meaning most of it is gone.</div>
<div>
<br /></div>
<div>
Displaying the console (F12 in chrome) revealed the problem:</div>
<div>
<i>Blocked script execution in 'http://10.10.10.10:8080/job/test/ws/run/report/index.html' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.</i></div>
<div>
<i><br /></i></div>
<h2>
Temporary solution:</h2>
<div>
<ul>
<li>Go to Jenkins main page</li>
<li>Click on Manage Jenkins</li>
<li>Click on Script Console</li>
<li>Paste the following and press run: <span style="background-color: white; color: #222426; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 15px; line-height: 19.5px;">System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")</span></li>
</ul>
</div>
<div>
Go again to the page the was "corrupted" and refresh it</div>
<div>
<br /></div>
<div>
<b>NOTICE: </b>This fix does not survive restart of Jenkins</div>
<div>
<br /></div>
<h2>
<b>Fixed Solution:</b></h2>
<h3>
Ubuntu</h3>
<div>
<ul>
<li>SSH the jenkins machine</li>
<li>Edit the jenkins default file located at /etc/default/jenkins</li>
<li>Search for JAVA_ARGS parameter</li>
<li>Add the following: -Dhudson.model.DirectoryBrowserSupport.CSP=\"\""</li>
<li>Restart jenkins</li>
</ul>
<h3>
CentOS</h3>
</div>
<div>
<br /></div>
<div>
<ul>
<li>SSH the jenkins machine</li>
<li>Edit the jenkins default file located at /etc/sysconfig/jenkins</li>
<li>Search for JENKINS_JAVA_OPTIONS parameter</li>
<li>Add the following: -Dhudson.model.DirectoryBrowserSupport.CSP=\"\""</li>
<li>Restart jenkins</li>
</ul>
</div>
<h2>
See this for more information</h2>
<div>
<a href="http://stackoverflow.com/questions/34315723/blocked-script-execution-in-url-because-the-documents-frame-is-sandboxed-and">http://stackoverflow.com/questions/34315723/blocked-script-execution-in-url-because-the-documents-frame-is-sandboxed-and</a></div>
<div>
<i><br /></i></div>
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0tag:blogger.com,1999:blog-4037837214026630684.post-76630330523586015112016-04-03T10:58:00.000+03:002016-04-03T10:58:46.866+03:00Checkout with SVNKIT on UNIX - Solving SVN authentication error E170001 <h2>
Prefix</h2>
<div>
I'm writing this post since I've seen many encountered this error without an answer including myself and SVNKIT documentation is not at best, for example:</div>
<div>
<br /></div>
<div>
<a href="http://stackoverflow.com/questions/26171365/svnkit-authentication-error-e170001-but-only-when-checking-out-and-only-on-one#_=_">http://stackoverflow.com/questions/26171365/svnkit-authentication-error-e170001-but-only-when-checking-out-and-only-on-one#_=_</a></div>
<div>
<a href="http://stackoverflow.com/questions/2784431/svnkit-not-working-on-unix">http://stackoverflow.com/questions/2784431/svnkit-not-working-on-unix</a></div>
<div>
<br /></div>
<h3>
Problem</h3>
<h4>
The following code will run okay in case you in the windows and working in the domain same as the SVN but will not work on UNIX platforms</h4>
<div>
<br /></div>
<div>
<div>
SvnOperationFactory svnOperationFactory = new SvnOperationFactory();</div>
<div>
SvnCheckout svnCheckout = svnOperationFactory.createCheckout();</div>
<div>
svnCheckout.setSingleTarget(SvnTarget.fromFile(checkoutFolder));</div>
<div>
svnCheckout.setSource(SvnTarget.fromURL(url));</div>
<div>
svnCheckout.run();</div>
</div>
<div>
<br /></div>
<h3>
Solution:</h3>
<h4>
Add the following line after creating the SvnOperationFactory</h4>
<div>
<br /></div>
<div>
<div>
svnOperationFactory.setAuthenticationManager(SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray()));</div>
<div>
<br />
<h3>
Complete code should look like this:</h3>
</div>
</div>
<div>
<div>
SvnOperationFactory svnOperationFactory = new SvnOperationFactory();</div>
<div>
svnOperationFactory.setAuthenticationManager(SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray()));</div>
<div>
SvnCheckout svnCheckout = svnOperationFactory.createCheckout();</div>
<div>
svnCheckout.setSingleTarget(SvnTarget.fromFile(checkoutFolder));</div>
<div>
svnCheckout.setSource(SvnTarget.fromURL(url));</div>
<div>
svnCheckout.run();</div>
</div>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0tag:blogger.com,1999:blog-4037837214026630684.post-78924275336852944782015-09-05T21:39:00.001+03:002015-09-05T21:41:09.973+03:00Connecting to Oracle Database using SQL Developer and Wallet authentication method<h2>
The story behind this post</h2>
<div dir="ltr">
A friend encountered a trouble opening a connection using SQL Developer to oracle database using Wallet authentication method.</div>
<div dir="ltr">
<br /></div>
<div dir="ltr">
The ORA error code we got was ORA-12578 TNS: wallet open failed.<br />
After an hour of trying to troubleshoot we were able to make it work.</div>
<div dir="ltr">
<br /></div>
<div dir="ltr">
This post documents how to use the Oracle Sql developer while using the wallet authentication </div>
<h2>
Assumptions</h2>
<div>
<ul>
<li>Your Wallet already setup at the database, if you need any help see <a href="http://www.dba-resources.com/oracle/a-guide-to-oracle-wallet/">http://www.dba-resources.com/oracle/a-guide-to-oracle-wallet/</a></li>
<ul>
<li>You should have the files cwallet.sso and ewallet.p12 files at the end of the process</li>
</ul>
<li>Your Wallet storage is setup on your Client (where the SQL developer is installed)</li>
<li>Your TNS is setup with the connection to the database, example set is:</li>
</ul>
TEST_WALLET_ALIAS = </div>
<div>
( DESCRIPTION = </div>
<div>
( ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.17.2)(PORT = 5138))</div>
<div>
( CONNECT_DATA = </div>
<div>
(SERVICE_DATA = demo_srv)</div>
<div>
)</div>
<div>
)<br />
<br />
<ul>
<li>Your sqlnet.ora have the needed configuration. Our at the client side sqlnet.ora looked like this:</li>
</ul>
<div>
WALLET_LOCATION =</div>
<div>
( SOURCE =</div>
<div>
( METHOD = FILE)</div>
<div>
( METHOD_DATA = </div>
<div>
( DIRECTORY = c:\app\product\11.2.0\client_2\network\admin)</div>
<div>
)</div>
<div>
)</div>
</div>
<div>
<br /></div>
<div dir="ltr">
</div>
<h2>
Preparing</h2>
<h3>
Verifying Wallet connection is working locally</h3>
<div>
Before trying to work remotely using the SQL developer, verify the wallet is working by doing the following (NOTICE: This requires access to the database machine itself. Not always you have that access rights. If you do not, try):</div>
<div>
<ol>
<li>Connect to the machine (using SSH or Remote Desktop)</li>
<li>Connect locally using the wallet authentication method using the following syntax:</li>
<ol>
<li>sqlplus /@<your wallet name> (For example sqlplus /@TEST_WALLET_ALIAS</li>
</ol>
<li>If you are able to connect on the database itself continue.</li>
</ol>
<h3>
Verifying Wallet connection works remotely using sqlplus</h3>
</div>
<div>
If you have installed the oracle client correctly you probably have the sqlplus client (Oracle's proprietary client).</div>
<div>
<br /></div>
<div>
We'll use it to verify the Wallet is working remotly</div>
<div>
<ol>
<li>Open terminal or command line</li>
<li>Go to your oracle client installation directory. Our's was at c:\app\product\11.2.0\client_2)</li>
<li>Go to the network admin directory.</li>
<li>Copy the sqlnet.ora, tnsnames.ora and wallet files from the database. The tnsnames.ora should have the alias, in our case it was TEST_WALLET_ALIAS</li>
<li>Connect using the syntax of the wallet authentication</li>
<ol>
<li>sqlplus /@<your wallet name> (For example sqlplus /@TEST_WALLET_ALIAS</li>
</ol>
</ol>
<h3>
Connecting using SQL Developer</h3>
</div>
<div>
If you've reached this point, I'm assuming sqlplus worked for you at least remotely.</div>
<div>
So, in order to make the SQL Developer use the wallet authentication we'll do the following</div>
<div>
<br /></div>
<div>
<ol>
<li>Verify that you have environment variable named TNS_ADMIN in the system environment variables pointing to the directory where the sqlnet.ora directory resides,</li>
<ol>
<li>To configure environment variables on windows, go to Control Panel --> System Properties --> Environment Variables</li>
</ol>
<li>In the sqlnet.ora you need to direct to wallet directory.</li>
<li>Create a wallet using the wallet manager</li>
<li>Copy the wallet files from the Oracle database to the wallet directory</li>
<li>Create a new advanced connection using the following format</li>
<ol>
<li>Connection Name: TestWaller (the name does not really matter)</li>
<li>Connection Type: Advanced</li>
<li>Custom JDBC URL: jdbc:oracle:oci /@<your wallet alias> (In our case it was jdbc:oracle:oci /@TEST_WALLET_ALIAS)</li>
</ol>
</ol>
</div>
<div>
<br /></div>
<h2>
Further reading</h2>
<div dir="ltr">
http://www.dba-resources.com/oracle/a-guide-to-oracle-wallet/</div>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0tag:blogger.com,1999:blog-4037837214026630684.post-22261578020161625122015-08-18T11:33:00.000+03:002015-08-18T11:33:33.582+03:00How to create a Hello World with IntelliJ and Aspect J<h2>
General</h2>
<div>
Since the whole AOP programming method is new to me AND it took me a while to understand how to work with AspectJ and IntelliJ I've decided to create this brief tutorial on how to setup simple Hello World project using AspectJ and IntellJ.</div>
<div>
<br /></div>
<h2>
The Ingredients </h2>
<div>
<ol>
<li>IntelliJ <span style="color: red;"><u style="font-weight: bold;">Ultimate</u> </span>(I used version 14, you can get it from here: https://www.jetbrains.com/idea/download/)</li>
<ol>
<li><b><span style="color: red;">NOTICE:</span></b> You must have the ultimate edition. I was not able to use AspectJ with community edition!</li>
<li>I hope you know how to install IntellJ, basically it's next next next.</li>
</ol>
<li>AspectJ installation (I've used version 1.8.6, you can get the installation JAR here http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.8.6.jar)</li>
<ol>
<li>You need to install the jar by double clicking on it in Windows or just run the command from command line 'java -jar aspectj-1.8.6.jar'</li>
<li><b><span style="color: red;">NOTICE:</span></b> Remember where the aspect installtion path since we'll need it later. In windows the default is c:\ (My installation was to c:\aspectj1.8)</li>
</ol>
</ol>
<h2>
So, let's start...</h2>
<h3>
Configuration</h3>
<div>
<ol>
<li>Start the IntelliJ</li>
<li>Click on Create new project</li>
<li>Choose Java project</li>
<li>Don't use template, just click next</li>
<li>Name the project AspectJDemoHelloWorld</li>
<li>Click Finish</li>
<li>Open the settings (in windows Ctrl+Alt+S)</li>
<li>Type compiler in the top search field, Chooe the Java Compiler. You should see something like this</li>
<ol>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-72LcN2OFOfE/VdLdWUtwLOI/AAAAAAAA6ME/Fx5vSn8qZJ0/s1600/2015-08-18%2B10_20_12-Settings.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="215" src="http://2.bp.blogspot.com/-72LcN2OFOfE/VdLdWUtwLOI/AAAAAAAA6ME/Fx5vSn8qZJ0/s320/2015-08-18%2B10_20_12-Settings.png" width="320" /></a></div>
</ol>
<li>Click on Use compiler and choose Ajc (That is the AspetJ compiler)</li>
<li>Click on the 'Path to Ajc compiler' browse button (near the Test button) and choose the 'aspectjtools.jar' at the lib directory of the AspectJ installation (In my case, C:\aspectj1.8\lib\aspectjtools.jar)</li>
<li>Click on the Test button. You should see something like this <div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-ITntVPyw6zc/VdLeM3WYPuI/AAAAAAAA6MQ/sDjkIuJYBMw/s1600/2015-08-18%2B10_25_45-AspectJDemoHelloWorld%2B-%2B%255BC__Work_AspectJDemoHelloWorld%255D%2B-%2BIntelliJ%2BIDEA%2B14.1.4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="214" src="http://2.bp.blogspot.com/-ITntVPyw6zc/VdLeM3WYPuI/AAAAAAAA6MQ/sDjkIuJYBMw/s320/2015-08-18%2B10_25_45-AspectJDemoHelloWorld%2B-%2B%255BC__Work_AspectJDemoHelloWorld%255D%2B-%2BIntelliJ%2BIDEA%2B14.1.4.png" width="320" /></a></div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
In the search field, type plugins. You should something like this</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-KuWalDBZZ6U/VdLpxkTiH0I/AAAAAAAA6Mg/_t-u_CPwwIg/s1600/2015-08-18%2B11_15_27-Settings.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="215" src="http://1.bp.blogspot.com/-KuWalDBZZ6U/VdLpxkTiH0I/AAAAAAAA6Mg/_t-u_CPwwIg/s320/2015-08-18%2B11_15_27-Settings.png" width="320" /></a></div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Click on the search field inside the plugins window and type AspectJ. Verify both plugins are enabled</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-o8t6uI32Jpg/VdLq7JKwglI/AAAAAAAA6Ms/kOcwbuvK158/s1600/2015-08-18%2B11_20_39-Settings.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="215" src="http://2.bp.blogspot.com/-o8t6uI32Jpg/VdLq7JKwglI/AAAAAAAA6Ms/kOcwbuvK158/s320/2015-08-18%2B11_20_39-Settings.png" width="320" /></a></div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Press ok</div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Open the project preferences (Ctrl + Alt + Shift + S in windows)</div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Click on Modules in the left navigation pane and then click Dependencies.</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-Ze0cGT1fcTs/VdLrVmmOLDI/AAAAAAAA6M0/oINq4YBzrLU/s1600/2015-08-18%2B11_22_02-Project%2BStructure.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="162" src="http://1.bp.blogspot.com/-Ze0cGT1fcTs/VdLrVmmOLDI/AAAAAAAA6M0/oINq4YBzrLU/s320/2015-08-18%2B11_22_02-Project%2BStructure.png" width="320" /></a></div>
</li>
<li>Click on the plus sign and choose JARs or directories.</li>
<li>Go to the installation folder of the AspectJ and choose the file 'aspectjrt.jar'<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-gIHUdllS8KY/VdLrr3nr-YI/AAAAAAAA6M8/CBa_nOYf_As/s1600/2015-08-18%2B11_23_58-Project%2BStructure.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="162" src="http://4.bp.blogspot.com/-gIHUdllS8KY/VdLrr3nr-YI/AAAAAAAA6M8/CBa_nOYf_As/s320/2015-08-18%2B11_23_58-Project%2BStructure.png" width="320" /></a></div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Press OK.</div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
That's for the configuration part</div>
</li>
</ol>
<h3>
Coding</h3>
<br />
<ol>
<li>Go to your src directory</li>
<li>Create a new JAVA class named Hello, Paste in the following</li>
<div>
<pre style="background-color: white; font-family: 'Courier New'; font-size: 9pt;"><span style="color: navy; font-weight: bold;">public class </span>Hello {
<span style="color: navy; font-weight: bold;">public static void </span>main(String[] args) {
<span style="font-style: italic;">sayHello</span>();
}
<span style="color: navy; font-weight: bold;">public static void </span>sayHello() {
System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.print(<span style="color: green; font-weight: bold;">"Hello "</span>);
}
}</pre>
<pre style="background-color: white; font-family: 'Courier New'; font-size: 9pt;"></pre>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<li>Create a new Aspect. Right click and choose Aspect (This ability was enabled due to adding of aspectjrt.jar)<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-d37su3mvNBQ/VdLsP13ozsI/AAAAAAAA6NE/pjIAgTpCoEM/s1600/2015-08-18%2B11_25_34-AspectJDemoHelloWorld%2B-%2B%255BC__Work_AspectJDemoHelloWorld%255D%2B-%2B%255BAspectJDemoHelloWorld.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="85" src="http://2.bp.blogspot.com/-d37su3mvNBQ/VdLsP13ozsI/AAAAAAAA6NE/pjIAgTpCoEM/s320/2015-08-18%2B11_25_34-AspectJDemoHelloWorld%2B-%2B%255BC__Work_AspectJDemoHelloWorld%255D%2B-%2B%255BAspectJDemoHelloWorld.png" width="320" /></a></div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Name the Aspect World and click ok</div>
</li>
<li><div class="separator" style="clear: both; text-align: left;">
Paste the following in the aspect</div>
</li>
<br />
<div class="separator" style="clear: both; text-align: left;">
<span style="color: navy; font-family: 'Courier New'; font-size: 9pt; font-weight: bold;">public aspect </span><span style="font-family: 'Courier New'; font-size: 9pt;">World {</span></div>
<br />
<pre style="background-color: white; font-family: 'Courier New'; font-size: 9pt;"> <span style="color: navy; font-weight: bold;">pointcut </span>greeting() : execution (* Hello.sayHello(..));
<span style="color: navy; font-weight: bold;">after</span>() <span style="color: navy; font-weight: bold;">returning</span>() : greeting() {
System.<span style="color: #660e7a; font-style: italic; font-weight: bold;">out</span>.println(<span style="color: green; font-weight: bold;">"World"</span>);
}
}</pre>
</div>
<li>Click on Build --> Rebuild project</li>
<li>Right click on Hello.java and choose Run</li>
<li>You should see 'Hello World' in the System console</li>
</ol>
<div>
If you only see Hello, that means the Aspect code was not compiled meaning, you should check the Ajc compiler is the compiler chosen (see step 7 in the configuration part)</div>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com2tag:blogger.com,1999:blog-4037837214026630684.post-50088884239069935062015-08-13T09:47:00.001+03:002015-08-18T11:33:58.699+03:00Creating javadocs without the package name before objects (with and without ant)<h2>
General</h2>
<br />
This is a simple one but from some reason it took a while to find the solution.<br />
<br />
When creatin javadocs, by default each object comes with it package name.<br />
If you don't want to see the package name before each object, use the noqualifier flag (thanks to<a href="https://www.blogger.com/%C2%A0http://stackoverflow.com/questions/10766238/javadoc-parameter-without-package-name"> http://stackoverflow.com/questions/10766238/javadoc-parameter-without-package-name</a> for the solution)<br />
<br />
<h2>
As ant target</h2>
<br />
Example Javadoc ant taget<br />
<pre style="background-color: white; font-family: 'Courier New'; font-size: 9pt;"></pre>
<pre style="background-color: white; font-family: 'Courier New'; font-size: 9pt;"><span style="background-color: #efefef;">
</span></pre>
<pre style="background-color: white; font-family: 'Courier New'; font-size: 9pt;"><span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">target </span><span style="background-color: #efefef; color: blue; font-weight: bold;">name</span><span style="background-color: #efefef; color: green; font-weight: bold;">="javadocs" </span><span style="background-color: #efefef;">></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">mkdir </span><span style="background-color: #efefef; color: blue; font-weight: bold;">dir</span><span style="background-color: #efefef; color: green; font-weight: bold;">="${dir.javadocs}"</span><span style="background-color: #efefef;">/></span>
<span style="background-color: #efefef;"><</span><span style="background-color: #efefef; color: navy; font-weight: bold;">javadoc </span><span style="background-color: #efefef; color: blue; font-weight: bold;">packagenames</span><span style="background-color: #efefef; color: green; font-weight: bold;">="com.tzachsolomon.example.*"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">sourcepath</span><span style="background-color: #efefef; color: green; font-weight: bold;">="-module1/src/main/java:module2/src/main/java"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">destdir</span><span style="background-color: #efefef; color: green; font-weight: bold;">="${dir.javadocs}"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">classpath</span><span style="background-color: #efefef; color: green; font-weight: bold;">="</span><span style="background-color: #efefef; color: green; font-size: 9pt; font-weight: bold;">module1/src/main/java:module2/src/main/java</span><span style="background-color: #efefef; color: green; font-size: 9pt; font-weight: bold;">"</span>
<span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">author</span><span style="background-color: #efefef; color: green; font-weight: bold;">="true"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">version</span><span style="background-color: #efefef; color: green; font-weight: bold;">="true"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">use</span><span style="background-color: #efefef; color: green; font-weight: bold;">="true"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">access</span><span style="background-color: #efefef; color: green; font-weight: bold;">="public"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">windowtitle</span><span style="background-color: #efefef; color: green; font-weight: bold;">="${app.name} API"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">encoding</span><span style="background-color: #efefef; color: green; font-weight: bold;">="UTF-8"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">verbose</span><span style="background-color: #efefef; color: green; font-weight: bold;">="false"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">noqualifier</span><span style="background-color: #efefef; color: green; font-weight: bold;">="all"</span><span style="background-color: #efefef; color: green; font-weight: bold;"> </span><span style="background-color: #efefef; color: blue; font-weight: bold;">doctitle</span><span style="background-color: #efefef; color: green; font-weight: bold;">="</span><span style="color: blue; font-weight: bold;">&lt;</span><span style="background-color: #efefef; color: green; font-weight: bold;">h1</span><span style="color: blue; font-weight: bold;">&gt;</span><span style="background-color: #efefef; color: green; font-weight: bold;">${app.name}</span><span style="color: blue; font-weight: bold;">&lt;</span><span style="background-color: #efefef; color: green; font-weight: bold;">/h1</span><span style="color: blue; font-weight: bold;">&gt;</span><span style="background-color: #efefef; color: green; font-weight: bold;">"</span><span style="background-color: #efefef;">/></span>
<span style="background-color: #efefef;"></</span><span style="background-color: #efefef; color: navy; font-weight: bold;">target</span><span style="background-color: #efefef;">></span></pre>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0tag:blogger.com,1999:blog-4037837214026630684.post-39909578242066973952015-05-21T23:22:00.001+03:002015-08-18T11:34:24.235+03:00JAVA - Separate log files for threads using log4j2<h2>
</h2>
<h2>
General</h2>
<div>
I've created this page to document how to create separate log file (in separate directory) using log4j2.<br />
It contains a small example (1 Main thread and 2 test threads), how to and explanation how it works.<br />
<br />
I hope you'll find it useful<br />
<br />
This tutorial is based on the post at <a href="http://stackoverflow.com/questions/25114526/log4j2-how-to-write-logs-to-separate-files-for-each-user">http://stackoverflow.com/questions/25114526/log4j2-how-to-write-logs-to-separate-files-for-each-user</a>!</div>
<h2>
Assumptions</h2>
<div>
<ol>
<li>You are familiar with JAVA</li>
<li>You are familiar with log4j2 configuration</li>
<li>You know how to link JARs (although I explain how to do it in IntelliJ)</li>
</ol>
<div>
<b><u>Still, if you need help since something is unclear don't hesitate to contact me :)</u></b></div>
<br />
<h2>
Prerequisites </h2>
<div>
<ol>
<li>Download log4j2 from <a href="https://logging.apache.org/log4j/2.0/download.html">https://logging.apache.org/log4j/2.0/download.html</a> (Current version 2.3) </li>
<li>Download slf4j from <a href="http://www.slf4j.org/download.html">http://www.slf4j.org/download.html</a> (Current version 1.7.12)</li>
</ol>
</div>
<br /></div>
<h2>
How to:</h2>
<div>
you download the project <a href="https://github.com/tzachs/test-log4j2-threads">https://github.com/tzachs/test-log4j2-threads</a> or do the following yourself</div>
<div>
<ol>
<li>Start IntelliJ</li>
<li>Create a new project named test-log4j2-threads</li>
<li>Create a directory named lib</li>
<li>Move the files apache-log4j-2.3-bin.tar.gz and slf4j-1.7.12.tar.gz to the lib directory</li>
<li>Extract the files in the directory</li>
<li>Create 3 classes</li>
<ol>
<li>Main.java</li>
<li>TestThread.java</li>
<li>MyLogger.java</li>
</ol>
<li>Create a file named log4j2.xml</li>
<li>Copy the following to Main.java</li>
<pre style="background: #000; color: #f8f8f8;"><span style="color: #e28964;">import</span> <span style="color: #99cf50;">org.apache.logging.log4j.ThreadContext</span>;
<span style="color: #aeaeae; font-style: italic;">/**
* Created by tzach on 5/16/15.
*/</span>
<span style="color: #99cf50;">public</span> <span style="color: #99cf50;">class</span> <span style="text-decoration: underline;">Main</span> {
<span style="color: #99cf50;">public</span> <span style="color: #99cf50;">static</span> <span style="color: #99cf50;">void</span> <span style="color: #89bdff;">main</span>(<span style="color: #99cf50;">String</span>[] <span style="color: #3e87e3;">args</span>) {
<span style="color: #99cf50;">ThreadContext</span><span style="color: #e28964;">.</span>put(<span style="color: #65b042;">"logFilename"</span>,<span style="color: #65b042;">"main"</span>);
<span style="color: #99cf50;">MyLogger</span><span style="color: #e28964;">.</span>init();
<span style="color: #99cf50;">int</span> i;
<span style="color: #99cf50;">MyLogger</span><span style="color: #e28964;">.</span>getLogger()<span style="color: #e28964;">.</span>info(<span style="color: #65b042;">"Started"</span>);
<span style="color: #99cf50;">TestThread</span> testThread;
testThread <span style="color: #e28964;">=</span> <span style="color: #e28964;">new</span> <span style="color: #99cf50;">TestThread</span>(<span style="color: #65b042;">"test1"</span>);
testThread<span style="color: #e28964;">.</span>start();
testThread <span style="color: #e28964;">=</span> <span style="color: #e28964;">new</span> <span style="color: #99cf50;">TestThread</span>(<span style="color: #65b042;">"test2"</span>);
testThread<span style="color: #e28964;">.</span>start();
<span style="color: #e28964;">for</span> ( i <span style="color: #e28964;">=</span> <span style="color: #3387cc;">0</span>; i <span style="color: #e28964;"><</span> <span style="color: #3387cc;">4</span>; i<span style="color: #e28964;">++</span>){
<span style="color: #e28964;">try</span> {
<span style="color: #99cf50;">MyLogger</span><span style="color: #e28964;">.</span>getLogger()<span style="color: #e28964;">.</span>debug(<span style="color: #65b042;">"GUI log"</span>);
<span style="color: #99cf50;">Thread</span><span style="color: #e28964;">.</span>sleep(<span style="color: #3387cc;">1000</span>);
} <span style="color: #e28964;">catch</span> (<span style="color: #99cf50;">InterruptedException</span> e) {
e<span style="color: #e28964;">.</span>printStackTrace();
}
}
<span style="color: #99cf50;">MyLogger</span><span style="color: #e28964;">.</span>getLogger()<span style="color: #e28964;">.</span>info(<span style="color: #65b042;">"Finished"</span>);
}
}
</pre>
<li>Copy the following code to TestThread.java</li>
<pre style="background: #000; color: #f8f8f8;"><span style="color: #e28964;">import</span> <span style="color: #99cf50;">org.apache.logging.log4j.ThreadContext</span>;
<span style="color: #aeaeae; font-style: italic;">/**
* Created by tzach on 5/16/15.
*/</span>
<span style="color: #99cf50;">public</span> <span style="color: #99cf50;">class</span> <span style="text-decoration: underline;">TestThread</span> <span style="color: #99cf50;">extends</span> <span style="color: #9b5c2e; font-style: italic;">Thread</span> <span style="color: #99cf50;">implements</span> <span style="color: #9b5c2e; font-style: italic;">Runnable</span> {
<span style="color: #99cf50;">private</span> <span style="color: #99cf50;">final</span> <span style="color: #99cf50;">String</span> threadName;
<span style="color: #99cf50;">public</span> <span style="color: #89bdff;">TestThread</span>(<span style="color: #99cf50;">String</span> <span style="color: #3e87e3;">threadName</span>){
<span style="color: #3e87e3;">this</span><span style="color: #e28964;">.</span>threadName <span style="color: #e28964;">=</span> threadName;
}
<span style="color: #99cf50;">@Override</span>
<span style="color: #99cf50;">public</span> <span style="color: #99cf50;">void</span> <span style="color: #89bdff;">run</span>() {
<span style="color: #99cf50;">Thread</span><span style="color: #e28964;">.</span>currentThread()<span style="color: #e28964;">.</span>setName(threadName);
<span style="color: #99cf50;">ThreadContext</span><span style="color: #e28964;">.</span>put(<span style="color: #65b042;">"logFilename"</span>, <span style="color: #99cf50;">Thread</span><span style="color: #e28964;">.</span>currentThread()<span style="color: #e28964;">.</span>getName());
<span style="color: #99cf50;">MyLogger</span><span style="color: #e28964;">.</span>getLogger()<span style="color: #e28964;">.</span>debug(<span style="color: #65b042;">"Starting new loop"</span>);
<span style="color: #99cf50;">int</span> i <span style="color: #e28964;">=</span> <span style="color: #3387cc;">0</span>;
<span style="color: #99cf50;">long</span> threadId <span style="color: #e28964;">=</span> <span style="color: #99cf50;">Thread</span><span style="color: #e28964;">.</span>currentThread()<span style="color: #e28964;">.</span>getId();
<span style="color: #e28964;">for</span> ( <span style="color: #99cf50;">int</span> z <span style="color: #e28964;">=</span> <span style="color: #3387cc;">0</span>; z <span style="color: #e28964;"><</span> <span style="color: #3387cc;">3</span>; z<span style="color: #e28964;">++</span>){
<span style="color: #99cf50;">MyLogger</span><span style="color: #e28964;">.</span>getLogger()<span style="color: #e28964;">.</span>warn(<span style="color: #65b042;">"Thread number: "</span> <span style="color: #e28964;">+</span> threadId <span style="color: #e28964;">+</span> <span style="color: #65b042;">" message number "</span> <span style="color: #e28964;">+</span> <span style="color: #e28964;">++</span>i);
<span style="color: #e28964;">try</span> {
<span style="color: #99cf50;">Thread</span><span style="color: #e28964;">.</span>sleep(<span style="color: #3387cc;">1000</span>);
} <span style="color: #e28964;">catch</span> (<span style="color: #99cf50;">InterruptedException</span> e) {
<span style="color: #99cf50;">MyLogger</span><span style="color: #e28964;">.</span>getLogger()<span style="color: #e28964;">.</span>error(e<span style="color: #e28964;">.</span>toString(),e);
}
}
}
}
</pre>
<li>Copy the following code to TestThread.java</li>
<pre style="background: #000; color: #f8f8f8;"><span style="color: #e28964;">import</span> <span style="color: #99cf50;">org.apache.logging.log4j.LogManager</span>;
<span style="color: #e28964;">import</span> <span style="color: #99cf50;">org.apache.logging.log4j.Logger</span>;
<span style="color: #aeaeae; font-style: italic;">/**
* Created by tzach on 5/16/15.
*/</span>
<span style="color: #99cf50;">public</span> <span style="color: #99cf50;">class</span> <span style="text-decoration: underline;">MyLogger</span> {
<span style="color: #99cf50;">private</span> <span style="color: #99cf50;">static</span> <span style="color: #99cf50;">Logger</span> logger;
<span style="color: #99cf50;">public</span> <span style="color: #99cf50;">static</span> <span style="color: #99cf50;">void</span> <span style="color: #89bdff;">init</span>(){
logger <span style="color: #e28964;">=</span> <span style="color: #99cf50;">LogManager</span><span style="color: #e28964;">.</span>getLogger(<span style="color: #65b042;">"test-log4j2-threads"</span>);
}
<span style="color: #99cf50;">public</span> <span style="color: #99cf50;">static</span> <span style="color: #99cf50;">Logger</span> <span style="color: #89bdff;">getLogger</span>(){
<span style="color: #e28964;">return</span> logger;
}
}
</pre>
<li>Copy the following to log4j2.xml</li>
<pre style="background: #000; color: #f8f8f8;"><span style="color: #89bdff;"><?<span style="color: #89bdff;">xml</span><span style="color: #89bdff;"> version</span>=<span style="color: #65b042;">"1.0"</span><span style="color: #89bdff;"> encoding</span>=<span style="color: #65b042;">"UTF-8"</span>?></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Configuration</span> <span style="color: #89bdff;">status</span>=<span style="color: #65b042;">"WARN"</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Appenders</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Console</span> <span style="color: #89bdff;">name</span>=<span style="color: #65b042;">"Console"</span> <span style="color: #89bdff;">target</span>=<span style="color: #65b042;">"SYSTEM_OUT"</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">PatternLayout</span> <span style="color: #89bdff;">pattern</span>=<span style="color: #65b042;">"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"</span>/></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">Console</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Routing</span> <span style="color: #89bdff;">name</span>=<span style="color: #65b042;">"RoutingAppender"</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Routes</span> <span style="color: #89bdff;">pattern</span>=<span style="color: #65b042;">"$${ctx:logFilename}"</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Route</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">RollingFile</span> <span style="color: #89bdff;">name</span>=<span style="color: #65b042;">"Rolling-${ctx:logFilename}"</span> <span style="color: #89bdff;">fileName</span>=<span style="color: #65b042;">"logs/${ctx:logFilename}/test.log"</span>
<span style="color: #89bdff;">filePattern</span>=<span style="color: #65b042;">"logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">PatternLayout</span> <span style="color: #89bdff;">pattern</span>=<span style="color: #65b042;">"%d{ABSOLUTE} %level{length=5} [%thread] %logger{1} - %msg%n"</span>/></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">SizeBasedTriggeringPolicy</span> <span style="color: #89bdff;">size</span>=<span style="color: #65b042;">"512"</span> /></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">RollingFile</span>></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">Route</span>></span>
<span style="color: #aeaeae; font-style: italic;"><!-- By having this set to ${ctx:logFileName} it will match when filename
is not set in the context --></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Route</span> <span style="color: #89bdff;">ref</span>=<span style="color: #65b042;">"Console"</span> <span style="color: #89bdff;">key</span>=<span style="color: #65b042;">"${ctx:logFileName}"</span>/></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">Routes</span>></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">Routing</span>></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">Appenders</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Loggers</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">Root</span> <span style="color: #89bdff;">level</span>=<span style="color: #65b042;">"trace"</span>></span>
<span style="color: #89bdff;"><<span style="color: #89bdff;">AppenderRef</span> <span style="color: #89bdff;">ref</span>=<span style="color: #65b042;">"RoutingAppender"</span>/></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">Root</span>></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">Loggers</span>></span>
<span style="color: #89bdff;"></<span style="color: #89bdff;">Configuration</span>></span>
</pre>
<li>Link the libraries. In Intellij you'll do it by doing the following:</li>
<ol>
<li>Open the Project Structure (Setting). In Ubunutu you can press Ctrl+Alt+Shift+S</li>
<li>Go to Modules</li>
<li>Press on the Dependencies tab</li>
<li>Click on the plus sign and add the JARs. You should see something like this in the end of the process.<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-asPhPdrEYec/VV444s_azHI/AAAAAAAAz0I/VEXb4Nh1-qQ/s1600/Screenshot%2Bfrom%2B2015-05-21%2B22%253A57%253A58.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="179" src="http://3.bp.blogspot.com/-asPhPdrEYec/VV444s_azHI/AAAAAAAAz0I/VEXb4Nh1-qQ/s320/Screenshot%2Bfrom%2B2015-05-21%2B22%253A57%253A58.png" width="320" /></a></div>
</li>
</ol>
<li>Click on build --> Make (Or Ctrl+F9)</li>
<li>Press on the run (or Shift+F10)</li>
<li>You should see a directory named logs created with the following structure:</li>
<ol>
<li>logs</li>
<ol>
<li>main</li>
<ol>
<li>test.log</li>
<ol>
</ol>
</ol>
<li>test1</li>
<ol>
<li>test.log</li>
<ol>
</ol>
</ol>
<li>test2</li>
<ol>
<li>test.log</li>
<ol>
</ol>
</ol>
</ol>
</ol>
<li>Notice that each test.log has messages only from it's own thread</li>
<li>That's it :)</li>
<ol>
</ol>
</ol>
<h2>
Explanation</h2>
</div>
<h3>
So what is actually going on?</h3>
<div>
As you can see, I've used Routing Appender which is named RoutingAppender in my example.</div>
<div>
The pattern to of route is depended on the variable in each Thread context named logFilename.</div>
<div>
<br /></div>
<div>
Notice that I do not handle any thing related to the creating of the files or the directories, it is all done by the log4j2.</div>
<div>
<br /></div>
<div>
All you need to do, is to put a name of the directory you want be created for each Thread, for example, Line number 9 in class Main.java:</div>
<pre style="background: #000; color: #f8f8f8;"><span style="color: #99cf50;">ThreadContext</span><span style="color: #e28964;">.</span>put(<span style="color: #65b042;">"logFilename"</span>,<span style="color: #65b042;">"main"</span>)
</pre>
<div>
<br />
By typing "main", I've decided that all the logs for the main thread will go to the directory logs/main<br />
<br />
For more info see <a href="https://logging.apache.org/log4j/2.0/faq.html#separate_log_files">https://logging.apache.org/log4j/2.0/faq.html#separate_log_files</a></div>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0tag:blogger.com,1999:blog-4037837214026630684.post-30365506376853362282013-02-09T00:02:00.001+02:002013-02-09T00:02:02.399+02:00How to alert user on change in database using AJAX, MYSQL, PHP?Hi,<br />
<br />
I have a simple web page where multiple users are editing the same rows at the same time.<br />
I needed a simple way to alert the users, if the page they were working on is no longer valid.<br />
Meaning, it was updated in the database and they should refresh the page (MY NEXT TASK IS TO AUTO UPDATE).<br />
<br />
This example document how to alert the user that the database has been changed and he/she should refresh the page.<br />
<br />
<b><u>Assumptions:</u></b><br />
1. You have a working MySQL database with the database named checkupdate<br />
2. In the checkupdate database there is a table named tbl1 with one integer column named _id<br />
<br />
<b><u>Let's start...</u></b><br />
<br />
Create the following files:<br />
index.html<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: #222222; font-size: 13px; line-height: 18px; overflow: auto; padding: 5px; width: 570px;"><code><span style="font-size: 12px; line-height: 14px;"><!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="checkupdate.js"> </script>
</head>
<body onload="process()">
Last update:
<div id="underInput" />
</body>
</html></span></code></pre>
<br />
checkupdate.js<br />
<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); overflow: auto; padding: 5px; width: 570px;"><span style="color: #222222;"><span style="font-size: 12px; line-height: 14px;">var xmlHttp = createXmlHttpRequestObject();
var nIntervalId;
var underInput;
function process () {
nIntervalId = setInterval(checkUpdate1, 10000);
checkUpdate1 ();
}
function checkUpdate1 () {
xmlHttp.open("GET", "checkupdate.php?t="+Math.random(), true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send();
}
function handleServerResponse () {
//console.log(xmlHttp.readyState);
underInput = encodeURIComponent(document.getElementById("underInput").innerHTML);
if ( xmlHttp.readyState==4 )
if ( xmlHttp.status==200) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.textContent;
message1 = parseInt(message);
underInput = parseInt(underInput);
if ( message1 > underInput ) {
alert ("Change was made in db, please refresh the page");
}
document.getElementById("underInput").innerHTML = message;
}
}
function createXmlHttpRequestObject() {
var xmlHttp;
if (window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsofot.XMLHTTP");
} catch (e) {
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
} catch (e) {
xmlHttp = false;
}
}
if (!xmlHttp) {
alert("Could not create XML Object");
} else {
return xmlHttp;
}
}</span></span><span style="color: #222222; font-size: 13px; line-height: 18px;">
</span></pre>
<div>
<br />
checkupdate.php<br />
<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); overflow: auto; padding: 5px; width: 570px;"><span style="color: #222222;"><span style="font-size: 12px; line-height: 14px;"><?php
$conn = mysql_connect('localhost','root','helloworld');
mysql_select_db('checkupdate', $conn);
$query = "SELECT MAX(_id) FROM tbl1";
$result = mysql_query($query,$conn);
$row = mysql_fetch_array($result);
mysql_close($conn);
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
echo "{$row['MAX(_id)']}";
echo '</response>';
?></span></span><span style="color: #222222; font-size: 13px; line-height: 18px;">
</span></pre>
<div>
<br /></div>
<br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com1tag:blogger.com,1999:blog-4037837214026630684.post-91142782992208195562013-02-07T22:28:00.001+02:002015-10-16T15:59:59.466+03:00AJAX, First Example (Based on the new Boston website)Hi,<br />
<br />
As I mentioned, this blog is mostly for me to remember the code I'm learning so I won't be doing much explaining of the code BUT however, I will place links in case I took the examples from some source.<br />
<br />
<b><u>Sources</u></b><br />
<br />
1. Example was basically taken from <a href="http://www.youtube.com/watch?v=tp3Gw-oWs2k">http://www.youtube.com/watch?v=tp3Gw-oWs2k</a>, I just change'd it a bit since it wasn't really working.<br />
2. <a href="http://www.w3schools.com/ajax/">http://www.w3schools.com/ajax/</a><br />
<br />
<b><u>Assumptions:</u></b><br />
1. You have a working web server supporting php<br />
2. You are familiar with PHP<br />
3. You are familiar with JavaScript<br />
<br />
<b><u>So lets start!</u></b><br />
1. Create a file name index.html and copy the code below to the file<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); overflow: auto; padding: 5px; width: 100%;"><code><span style="font-size: 12px; line-height: 14px;"><!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="foodstore.js"> </script>
</head>
<body>
<input type="text" id="userInput" onkeyup="process()" />
<div id="underInput" />
</body>
</html></span></code><span style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace;"><span style="font-size: 12px; line-height: 14px;">
</span></span></pre>
<br />
2. Create a file name foodstore.js and copy the code below to the file<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); overflow: auto; padding: 5px; width: 100%;"><code><span style="font-size: 12px; line-height: 14px;">var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject() {
var xmlHttp;
if (window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
} catch (e) {
xmlHttp = false;
}
}
if (!xmlHttp) {
alert("Could not create XML Object");
} else {
return xmlHttp;
}
}
function process() {
food = encodeURIComponent(document.getElementById("userInput").value);
//alert(food);
xmlHttp.open("GET", "foodstore.php?food="+food, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send();
}
function handleServerResponse () {
if ( xmlHttp.readyState==4 )
if ( xmlHttp.status==200 || xmlHttp.status == 206) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.textContent;
document.getElementById("underInput").innerHTML = '<span style="color:blue">' + message + '</span>';
}
}</span></code><span style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace;"><span style="font-size: 12px; line-height: 14px;">
</span></span></pre>
<br />
3. Create a file name foodstore.php and copy the code below to the file<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); overflow: auto; padding: 5px; width: 100%;"><span style="font-size: 12px; line-height: 14px;"><?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food=$_GET['food'];
$foodArray = array('tuna','bacon','beef','loaf','ham');
if (in_array($food,$foodArray)) {
echo "We do have ".$food;
} elseif ($food==''){
echo "Enter food";
} else {
echo "Sorry, we do not have ".$food;
}
echo '</response>';
?></span><span style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace;"><span style="font-size: 12px; line-height: 14px;">
</span></span></pre>
<br />
4. Place the 3 files in a directory under your web server (meaning you can browse it)<br />
<br />
5. Surf to the index.html file.<br />
<br />
6.You should see text box. Type your name in it and you should see something like this below the text box:<br />
<span style="color: blue;">Sorry, we do not have Tzach</span><br />
<br />
7. Type in tune and you should see the following:<br />
<span style="color: blue;">We do have tuna</span>Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com14tag:blogger.com,1999:blog-4037837214026630684.post-87202195731542420332013-02-07T22:08:00.003+02:002013-02-07T22:08:45.360+02:00Hello World?!Hi,<br />
<br />
My name is Tzach, 30 years old and I know it's kinda corny but I though to just type Hello World as a good example of posts in the blog.<br />
<br />
The whole purpose of this blog is to save and remember code examples or solutions to problems I've encountered during work or coding at home.<br />
<br />
I hope that the code and explanations will be coherent enough so that it might use others.<br />
<br />
Till next time,<br />
Tzach<br />
<br />Anonymoushttp://www.blogger.com/profile/04411996559633605683noreply@blogger.com0