Skip to main content

Posts

Java 8 JMX Default Metrics

This is more of a note. Here you can find default types and attributes for JMX on top of Java 8. Code: I will clean and explain it later :( private static void WriteAttributes(final MBeanServer mBeanServer, final ObjectName http) throws InstanceNotFoundException, IntrospectionException, ReflectionException { MBeanInfo info = mBeanServer.getMBeanInfo(http); MBeanAttributeInfo[] attrInfo = info.getAttributes(); System.out.println("Attributes for object: " + http +":\n"); for (MBeanAttributeInfo attr : attrInfo) { System.out.println(" " + attr.getName() + "\n"); } } Attributes for object: java.lang:type=MemoryPool,name=Metaspace:   Name   Type   Valid   Usage   PeakUsage   MemoryManagerNames   UsageThreshold   UsageThresholdExceeded   UsageThresholdCount   UsageThresholdSupported   CollectionUsageThreshold   Collectio...

How to set JAVA_HOME environment variable on Mac

Note : This tip is for Mac OSX 10.5 or later Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home , just export $JAVA_HOME in file ~/. bash_profile You might not have .bash_profile present on your system already. No worries we will create/update the profile in next step. Open Terminal: $ vi ~/.bash_profile Initially the vi editor will not be in edit mode, press 'i'. Update file by exporting variable: export JAVA_HOME=$(/usr/libexec/java_home) to the file (in the vi editor which we opened using the previous command) Saving and Closing the .bash_profile file opened inside vim editor: Press escape (while still inside the vi editor) type ":wq!" or press "Shift + zz" to save and exit the file.

Laravel XAMPP MySQL artisan migrate install error mysql.sock

In my previous post I wrote about setting up Laravel 4 on Mac with XAMPP . When I tried to use migrations (using artisan) I faced some issue. I will also post about Laravel migrations soon.    php artisan migrate:install Error :                                                     [PDOException]                                       SQLSTATE[HY000] [2002] No such file or directory                                              Solution : We need to tell artisan which mysql we want it to use. For this to work we need to porivde it with mysql.sock which we want it to use. So change your database settings like this: 'mysql' => array...

Working with MySQL on MAC. GUI Client, Web Based and Terminal

MySQL is most popular open source relational database. I have been using it myself for my PHP, Python and Ruby projects. Obviously, we need some client(s) to access MySQL database. In this post I am going to list down few clients.  Environment: I am using XAMPP on Mac. You can install MySQL separately if you want to. 1. Desktop GUI client Use SequelPro it is a nice tool and free.  Set it up and you are ready to go. 2. PHPMyAdmin phpMyAdmin is a free software tool written in  PHP , intended to handle the administration of  MySQL  over the Web. It gets setup with XAMPP.  3. Terminal Open up terminal and execute following commands -  Go to the directory with mysql binaries cd /Applications/XAMPP/xamppfiles/bin; Access mysql command line tool using ./mysql --user=root --password=

Setting Up Laravel 4.x on Mac OSX 10.8+ with XAMPP installed

I am new to PHP and have coded earlier with CodeIgniter. It is really easy to get started with CodeIgniter as it is light but powerful and has less features. As CodeIgniter is slowly phasing out I thought of moving to Laravel. Yes, it is powerful and AWESOME!!!  Lets get down to business.  1. Install composer I was doing it for the first time and faced issues: Some settings on your machine make Composer unable to work properly. Make sure that you fix the issues listed below and run this script again: The detect_unicode setting must be disabled. Add the following to the end of your `php.ini`: detect_unicode = Off A php.ini file does not exist. You will have to create one. I was clueless at this moment and started to search. After some search I found an article which tells about setting up AMP (Apache, My SQL and PHP) environment . While going through it I realized that I have Apache already installed on my system (I am new to Mac too). I followed the steps to co...

Create custom UITextFields and UITextViews with underline

Download In a recent iOS project I needed to create custom UITextView and UITextField. The look and feel had to be same as the Notes app, i.e. the text view and text field both should have horizontal lines as are present in a note. How to create a custom UIView To create a custom UIView we need to inherit from UIView class. We then need to override drawRect method. Override only if you need to change the way view is drawn as this is a performance intensive operation. Custom UITextField In order to create custom UITextField I inherited my class from UITextField. I need to draw a bottom border for this I need override drawRect method. //Get the current drawing context CGContextRef context = UIGraphicsGetCurrentContext(); //Set the line color and width CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); CGContextSetLineWidth(context, 0.5f); //Start a new Path CGContextBeginPath(context); // offset lines up -...

UITableView basic tutorial

In this tutorial we will create a table view with some dummy data, we will use disclosure indicator, change background color of cell, handle user selection, change separator color etc. There many tutorials which provides in depth information on UITableViews thus I will not touched that and instead targeted the practical How Tos. Create a new single view project and add a UITableView to the initial view controller which has been added to the storyboard automatically. In order to use UITableView in your viewcontroller you need to adapt to UITableViewDataSource and UITableViewDelegate.  UITableViewDelegate provides us with methods which let us handle user selection events, define row height, modifying view and much more. UITableViewDataSource provides data to UITableView to construct the view (to render content). Create table view cells, define the number of sections, number of rows in sections and much more. Adapt to required protocols @i...

Send Mail .NET

Allowing user to send mail (feedback, contact etc) is a very common feature in current web applications. In this series of post we will talk about some basic mail features and how to implement them in c#. Basics In order to send mail we need SMTP server configured as mails work over SMTP. You can configure SMTP server yourself or if you are using web hosting services you can get an account setup for you. In order to receive mails we need POP. For this tutorial series we will use gmail SMTP server.  .NET APIs .Net provides us with lots of classes present in System.Net.Mail to send an email. We will be using MailMessage, MailAddress and SmtpClient classes. We will create a MailHelper and will use it in Winforms, Asp.Net, Console etc. Mail Helper using System.Collections.Generic; using System.Net.Mail; namespace MailConsole { public class MailHelper { public static void SendMail(string subject,string message,List to) { var m...

Knowing Singelton

Singelton pattern in one of the most commonly used and talked about design pattern . Many appreciates it usefulness and many advocate against it. In this post we are not going to discuss all that but will concentrate on its implementation in c#. Type: It is a creational design pattern . Intent: It ensures that only one instance of the class exists. Usage: It is generally used when only one instance of a class is required. Thus, it can be seen to be used in logging frameworks, configuration objects etc. The ownership of instance creation lies with the class itself. This is because we can not ensure that only one instance will ever exists for the class if the responsibility lies with someone else. The class will instantiate the object when it will be used for the first time. This also ensures lazy initialization. Point to consider: Singelton object is mostly made accessible globally and thus being abused by being used as global variable. We must keep in mind that not onl...

C# Polymorphism - handle with care

Download Overriding is the base for most of the design patterns which exists. It provides us with an essential tool called  Polymorphism  . What is  Polymorphism  ? Polymorphism means one interface and many forms. It is a characteristics of being able to assign a different meaning or usage to something in different contexts specifically to allow an entity such as a variable, a function or an object to have more than one form.  There are two types of Polymorphism.  Compile time:  function or operator overloading  Runtime:  Inheritence & virtual functions Here, we are going to talk about the Runtime Polymorphism.  If you read the definition carefully you will see that there is going to be some type casting and compiler is going to made some assumptions. These assumptions may fail on execution. Let me explain this further. Type casting: There will be a parent interface (Interface, abstract class, class) and may be ...

c# reference types passed by value or reference

Download Time and again I have heard that in C#, method arguments are passed based upon there type. i.e. Value types are passed by value and reference types are passed by reference. This is so untrue. Reference types have nothing to do with pass by value. Let us talk some basic. Consider the assignment first. When you assign a value type to another its value is copied. This is because value type variable contains its data directly. When you assign a reference type variable to another only the reference gets copied. This is because a reference type variable does not contains its data directly. It only holds reference to data. Thus, when we pass a value type, its value gets copied and when we pass a reference type its reference gets copied. That is why if we change the value of a value type variable in the block (method), the change is not seen outside of it. On the other hand if we change the value of member of reference passed to the block from within the block the cha...

Callback in c#

Download Running tasks in background is the need of the hour when working on real world applications. There are few tasks which we can fire and forget but for few of them we will like to receive the feedback. This is when callbacks comes into the picture and provides a channel for these objects to communicate with each other. These tasks are very common in desktop and mobile applications. In this post we will talk about callback mechanism in c#. In future post we will build upon this and do lot more. Using interfaces for callbacks in c# using System; using System.Threading; namespace Callbacks { class Program { static void Main(string[] args) { var consumer = new Consumer(); consumer.DoWork(); Console.ReadLine(); } } interface IOnProcessCompleteListener { void OnProcessComplete(string message); } internal class Consumer: IOnProcessCompleteListener { private Work...

Android - Disable Text Selection

In order to disable text selection in a WebView we can use following approaches: CSS * {   user-select: none; -moz-user-select: -moz-none; -khtml-user-select: none; -webkit-user-select: none; } This approach will not work on all the versions of Android as the support was not there till late. Handling LongClick public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     WebView webView = (WebView) this.findViewById(R.id.webView1);     webView.loadData("<html><head><style>head><body>You scored           <b>192</b>     points.</body></html>", "text/html", null);                     webView.setOnLongClickListener(new View.OnLongClickListener() {         public boolean onL...

Split String in C++ with delimiters

In order to split string in c++ we can use strtok(). A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters. On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.  Read More Here #include <iostream.h> #include <string.h> int main( int argc, char ** argv) {      // multiple delimiters can be provided      char const delimiters[] = "/:" ;          // initial string to split        std::string sCFIString = "/6/2702!/4/6/6/122/1:3" ;   ...

Convert std::string to char* and const char* - c++

Convert std::string to const char* std :: string name = "Anuj" ; const char * constName = name.c_str(); Convert std::string to char* std :: string name = "Anuj" ; const char * constName = name.c_str(); char * ptrToName = const_cast< char *> (name.c_str()); A simple progam  #include <iostream> #include <stdio.h> using namespace std; int main() {     std :: string name = "Anuj" ;      const char * constName = name.c_str();      char * ptrToName = const_cast< char *> (name.c_str());     printf( "%s" ,constName);     printf( "%s" ,ptrToName);     cout << name << endl;      return 0 ; }