Category: Tips

Share files to download from tomcat

Sharing files with friends or colleagues who are in same network is every easy now. All you need is tomcat and name of your computer. So let’s see how we can do it.

I’m assuming tomcat installation be tomcat=”C:\tomcat”

  1. Download Apache Tomcat & Java. Configure environmental variable “JAVA_HOME” with path for Java installation directory and “CATALINA_HOME” with path for Tomcat installation directory.
  2. Go to tomcat/bin and run “startup” batch file. check from browser whether you able to access tomcat home page(http://localhost:8080). if you reach here it means your tomcat installation is good, now lets do some setup.
  3. Go to folder that you want to share and create a simple web.xml file in it.
  4. Go to tomcat/conf open server.xml file and add context for the directory that you want to share like “<Context docBase=”D:\Personal\Tutorials” path=”/Tutorials” reloadable=”true” source=”D:\Personal\Tutorials”/>”
  5. Go to tomcat/conf open web.xml, search for “listings” in “init-param” tag you will find param-name as “listings” change its corresponding param-value to “true

That’s all now save all the changes and run tomcat, folder you shared will be available in the path you mentioned in step 4.

For example in my case directory is available at http://localhost:8080/Tutorials

 

Advertisements

Shared Git repository on LAN

Few days back me along with my colleagues came up with an idea to do POC on healthcare mobile app using ionic. I thought of creating a common code repository in one of our systems and share that code repository using office LAN. A part from pull and push commands in git I didn’t know anything. So obviously like any other developer I googled, after lot of research I successfully created a Git repository in our LAN. Here are the steps

Create a bare repository (Bare repository is the one that has no working tree. It means its whole contents is what you have in .git directory. You can only commit to bare repository by pushing to it from your local clone. It has no working tree, so it has no files modified, no changes)

 git init –bare project1.git

Share it to the users on LAN

Clone this project in some other location on our system using command.

git clone //192.168.xxx.xxx/xx/xx/project1.git

Create you project setup files here or dump project into cloned project1 folder and Add the files to cloned repository using

git add .

Then commit the files using

git commit -m “First commit”

At this point your bare repository doesn’t have any branches, when you commit the files in our local cloned project repository it creates a master branch local to our codebase.

Push the changes to the newly created master branch in local code using

git push

The default branch of bare repository is “origin” and default branch of your local cloned repository is “master”. You can check it by commands from cloned repository

git remote (returns origin)

git branch (returns master)

Now it’s very simple, we know the branch of our remote repository & our local branch. Now we need to push the local changes on master to origin.

git push [remote-repository-name] [local-branch-name]

git push origin master

It’s done !! Now any one who has access to project1.git can simply clone the project using command

git clone //192.168.xxx.xx/*/*/project1.git

Finally we can use the most used command pull & push to do our daily activities on code base. Hope this helps.

AngularJs Tip: Don’t use $watch on $scope

Try to use “ng-change” instead of “$watch” when you want to watch a particular value. There are couple of benefits like performance improvement because of no overhead on digest cycle & writing test cases for ng-change is very easy compared to writing them on $watch.

AngularJs Tip: bootstrap !!

  1. Using ngApp will auto bootstrap your application.
  2. Only one AngularJs application can be auto-bootstraped for one html document. To run multiple applications in an html document we must manually bootstrap them using angular.bootstrap
  3. AngularJs applications cannot be nested within each other.

Javascript Tip: How to find whether object is window object or not

For the object to be window object. It should pass the below condition

obj && obj.document && obj.location && obj.alert && obj.setInterval

AngularJs Note: $injector Vs $inject Vs Injector()

Following are the key differences:

 

#injector: Used to invoke or get dependencies into variables

   $injector.get('A')
   //gets the implementation of service called 'A' 

   $injector.invoke('A')
   //returns the value of implementation of service 'A'

#$inject: Used to inject dependencies into a function arguments

   var test = function('A', 'B'){} 
   test.$inject = ['A', 'B']
   $injector.invoke(explicit)

#injector(): Used on document object which is mainly used to bring non-angular environment code into angular environment.

   var $div = $('<div ng-controller="MyCtrl">{{content.label}}</div>'); 
   $(document.body).append($div); 
   angular.element(document).injector().invoke(function($compile) { 
     var scope = angular.element($div).scope(); 
     $compile($div)(scope); 
   });

AngularJs: Understand DI & $injector

We all know in AngularJs requried components are Dependency Injected. For example, if we need $scope in controller, we simply inject ‘$scope’ as one of the parameters to the controller function. We are least bothered how we $scope object was created and given to us. This is called Dependency Injection.

# Dependency Injection is acheived by using $injector & $provider in AngularJs. I will try to cover most of the important points related to $injector in this post.

In general Dependency Injection is achieved by annotations i.e using ‘@’ example spring framework. However Javascript doesn’t support annotations. In Order to provide DI angular uses a service called $injector. return object for $injector looks like
$provider_return_function
However $injector.invoke calls annotate & get methods to resolve dependencies.

$injector#get – Returns an instance of the service.

$injector#invoke – Invoke the method and supply the method arguments from the `$injector`returns invoked function results

$injector#has – Checks whether particular service exists or not

$injector#annotate – Returns an array of service names which the function is requesting for injection. This API is used by the injector to determine which services need to be injected into the function when the function is invoked. There are three ways in which the function can be annotated with the needed dependencies

# Argument names
The simplest form is to extract the dependencies from the arguments of the function. This is done by converting the function into a string using `toString()` method and extracting the argument names.

 
   // Given
   function MyController($scope, $route) {
     // ...
   }

   // Then
   expect(injector.annotate(MyController)).toEqual(['$scope', '$route']);

# The ‘$inject’ property
If a function has an ‘$inject’ property and its value is an array of strings, then the strings represent names of services to be injected into the function.

  // Given
  var MyController = function(obfuscatedScope, obfuscatedRoute) {
    // ...
  }
  // Define function dependencies
  MyController['$inject'] = ['$scope', '$route'];

  // Then
  expect(injector.annotate(MyController)).toEqual(['$scope', '$route']);

# The array notation
It is often desirable to in-line Injected functions and that’s when setting the `$inject` property is very inconvenient. In these situations using the array notation to specify the dependencies in a way that survives minification is a better choice:

  injector.invoke(['$compile', '$rootScope', function(obfCompile, obfRootScope) {
     // ...
  }]);

 // Therefore
 expect(injector.annotate(
    ['$compile', '$rootScope', function(obfus_$compile, obfus_$rootScope) {}])
 ).toEqual(['$compile', '$rootScope']);</pre>

This is how Dependency Injection is done in AngularJs.

Sometimes you want to get access to the injector of a currently running Angular app from outside Angular. Perhaps, you want to inject and compile some markup after the application has been bootstrapped. We can do this using the extra ‘injector()’ present document object.

In the following example a new block of HTML containing a ‘ng-controller’ directive is added to the end of the document body by JQuery. We then compile and link it into the current AngularJS scope.

   var $div = $('<div>{{content.label}}</div>');
   $(document.body).append($div);
   angular.element(document).injector().invoke(function($compile) {
     var scope = angular.element($div).scope();
     $compile($div)(scope);
   });

** All the examples and information taken from angular.js