working on it ...

Filters

Explore Public Snippets

Sort by

Found 62k snippets matching: example

    public by micurs modified Mar 4, 2014  6863148  307  9  18

    Typescript: passing a class member function as parameter to another function

    Typescript introduces the class construct and the concept of member functions. However if you try to pass a instance member function as parameter to another function the binding with the instance is lost. To solve this problem you can use the capability of javascript (and Typescript) to support closures on anonymous function (or lambda expressio
    ///<reference path='../types/node.d.ts'/>
    ///<reference path='../types/underscore.d.ts'/>
    
    import _ = require("underscore");
    
    class SomePeople {
      values : string[] = [ 'John', 'Mary', 'Peter'];
    
      out() {
        _.each( this.values, (v) => process.stdout.write( v + '\n') );
      }
    }
    // This function accept a function it will execute internally
    function outSomething( outFunc : () => any ) {
      outFunc();
    }
    
    var crowd = new SomePeople();
    
    // This won't work since the actual crowd instance binding will be lost
    outSomething( crowd.out );
    
    // This will work: the closure within the lambda expression retain the
    // crowd instance and its data
    outSomething( () => crowd.out() );
        

    public by cghersi modified Aug 20, 2013  468164  7  8  2

    Load a SOAP endpoint with Apache CXF using JMS transport provided by Apache ActiveMQ

    This is an example on how to load a SOAP web service in order to be able to invoke its methods via JMS transport
    private JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    public MyInterface loadEndPoint() {
    	//try to load from local, if available:
    	boolean loadOK = false;
    	MyInterface result = null;
    	try {
    		Class<?> c = Class.forName("MyImplClass");
    		Method fact = c.getMethod("get");
    		result = (MyInterface) fact.invoke(null);
    		loadOK = true;
    	} catch (ClassNotFoundException e) {
    		log.info("Cannot find MyImplClass, redirecting to  remote access...", e);		
    	} catch (Exception e) {
    		log.warn("Error searching MyImplClass, , redirecting to remote access. Exception is " + e, e);
    	}
    		
    	//load remote endpoint:
    	if (!loadOK) {
    		//detect transport protocol for web services:
    		String brokerAddr = "127.0.0.1"; //the right broker address
    		String queueName = "MyQueueName";
    		String address = "jms:jndi:dynamicQueues/" + queueName + "?jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory&jndiConnectionFactoryName=ConnectionFactory&jndiURL=" + brokerAddr;
    
    		log.info("Connecting to " + address);
    		String connString = "tcp://" + brokerAddr + ":61616";
    		ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory("Myusername", "Mypassword", connString);
    
    		SoapBindingConfiguration conf = new SoapBindingConfiguration();
    		//MTOM not supported! .net does not interoperate with cxf mtom impl.
    //		conf.setMtomEnabled(true);
    		conf.setVersion(Soap12.getInstance());
    		factory.setBindingConfig(conf);
    
    		factory.setServiceClass(MyInterface.class);
    		factory.setAddress(address);
    		factory.getFeatures().add(new WSAddressingFeature());
    		factory.getFeatures().add(getJMSConfigFeature(queueName, connFactory, 10000)));
    		Object resObj = factory.create();
    		if ((resObj != null) && (resObj instanceof MyInterface))
    			result = (MyInterface) resObj;
    		}
    
    		return result;
    	}
    }
    
    public static JMSConfigFeature getJMSConfigFeature(String queueName, ActiveMQConnectionFactory connectionFactory, Long receiveTimeout) {
    	JMSConfigFeature jmsConfigFeature = new JMSConfigFeature();
    	JMSConfiguration jmsConfig = new JMSConfiguration();
    	jmsConfig.setUseJms11(true);
    	jmsConfig.setTargetDestination(queueName);
    	jmsConfig.setConnectionFactory(connectionFactory);
    	if (receiveTimeout != null)
    		jmsConfig.setReceiveTimeout(receiveTimeout);
    	jmsConfigFeature.setJmsConfig(jmsConfig);
    	return jmsConfigFeature;
    }

    public by przodownikR1 modified Jul 21, 2015  394522  0  3  0

    example create datasource in javaConfig with full desc and params

    example create datasource in javaConfig with full desc and params: db_config_full.java
    @Bean
      public DataSource dataSource() {
        final DataSource dataSource = new DataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        dataSource.setTestOnBorrow(true);
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnReturn(false);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setValidationInterval(30000);
        dataSource.setTimeBetweenEvictionRunsMillis(5000);
        dataSource.setMaxActive(100);
        dataSource.setMinIdle(10);
        dataSource.setMaxWait(10000);
        dataSource.setInitialSize(10);
        dataSource.setRemoveAbandonedTimeout(3600);
        dataSource.setRemoveAbandoned(true);
        dataSource.setLogAbandoned(true);
        dataSource.setMinEvictableIdleTimeMillis(30000);
        return dataSource;
      }
    
    

    public by Derek Pauley modified Sep 29, 2015  381087  17  3  1

    SVNKit export from Subversion example

    SVNKit export from Subversion example: SvnExportExample.java
    import java.io.File;
    
    import org.tmatesoft.svn.core.SVNDepth;
    import org.tmatesoft.svn.core.SVNException;
    import org.tmatesoft.svn.core.SVNURL;
    import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
    import org.tmatesoft.svn.core.io.SVNRepository;
    import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
    import org.tmatesoft.svn.core.wc.SVNClientManager;
    import org.tmatesoft.svn.core.wc.SVNRevision;
    import org.tmatesoft.svn.core.wc.SVNUpdateClient;
    import org.tmatesoft.svn.core.wc.SVNWCUtil;
    
    
    public class SvnExportExample {
    
    	public static void main(String... args){
    		final String url = "svn://cavcops01.global.local/skunkworks";
    		final String destPath = "c:/temp/svntest";
    		
    		SVNRepository repository = null;
    		
    		try{
    			//initiate the reporitory from the url
    			repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
    			//create authentication data
    			ISVNAuthenticationManager authManager = 
    					SVNWCUtil.createDefaultAuthenticationManager("cliff.darling", "svnuser");
    			repository.setAuthenticationManager(authManager);
    			//output some data to verify connection
    			System.out.println( "Repository Root: " + repository.getRepositoryRoot( true ) );
    			System.out.println(  "Repository UUID: " + repository.getRepositoryUUID( true ) );
    			//need to identify latest revision
    			long latestRevision = repository.getLatestRevision();
    			System.out.println(  "Repository Latest Revision: " + latestRevision);
    			
    			//create client manager and set authentication
    			SVNClientManager ourClientManager = SVNClientManager.newInstance();
    			ourClientManager.setAuthenticationManager(authManager);
    			//use SVNUpdateClient to do the export
    			SVNUpdateClient updateClient = ourClientManager.getUpdateClient( );
    			updateClient.setIgnoreExternals( false );
    			updateClient.doExport( repository.getLocation(), new File(destPath), 
    					SVNRevision.create(latestRevision), SVNRevision.create(latestRevision), 
    					null, true, SVNDepth.INFINITY);
    			
    		} catch (SVNException e) {
    			e.printStackTrace();
    		}finally {
    			System.out.println("Done");
    		}
    	}
    }
    
    
    

    public by lbottaro modified Mar 24, 2014  406071  1  7  9

    Simple CORBA java server

    This code shows an example of simple corba server in java. A CORBA server needs a local ORB object. The server instantiates an ORB and registers its servant objects so that the ORB can find the server when it receives an invocation for it. the server is a process that instantiates one or more servant objects. Each servant implements the
    // The package containing our stubs.
    import HelloApp.*;
    
    // HelloServer will use the naming service.
    import org.omg.CosNaming.*;
    
    // The package containing special exceptions thrown by the name service.
    import org.omg.CosNaming.NamingContextPackage.*;
    
    // All CORBA applications need these classes.
    import org.omg.CORBA.*;
    
    public class HelloServer 
    {
      public static void main(String args[])
      {
        try{
           // Create and initialize the ORB
          ORB orb = ORB.init(args, null);
          
          // Create the servant and register it with the ORB
          HelloServant helloRef = new HelloServant();
          orb.connect(helloRef);
          
          // Get the root naming context
        org.omg.CORBA.Object objRef = 
    orb.resolve_initial_references("NameService");
          NamingContext ncRef = NamingContextHelper.narrow(objRef);
          
          // Bind the object reference in naming
          NameComponent nc = new NameComponent("Hello", "");
          NameComponent path[] = {nc};
          ncRef.rebind(path, helloRef);
          
          // Wait for invocations from clients
          java.lang.Object sync = new java.lang.Object();
          synchronized(sync){
            sync.wait();
          }
              } catch(Exception e) {
            System.err.println("ERROR: " + e);
            e.printStackTrace(System.out);
          }  
      }
    }
    
    class HelloServant extends _HelloImplBase
    {
      public String sayHello()
      {
        return "\nHello world!!\n";
        }
    }
        

    public by lbottaro modified Mar 31, 2014  355661  0  7  3

    Vendor class - Java example for marshalling/unmarshalling, using JAX-B XmlID annotation

    This is the Vendor class, where the XmlElement annotation sets the vendorID attribute as XmlID. Such key will match the reference set in Card class, using annotation XmlIDREF.
    package com.test.jaxb;
    
    import java.io.Serializable;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlID;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Vendor implements Serializable {
    	
    	 @XmlElement(name = "vendor-ID")
    	 @XmlID
    	private String vendorID;
    	 @XmlElement(name = "vendor-Name")
    	private String vendorName;
    	/**
    	 * @param vendorID
    	 * @param vendorName
    	 */
    	public Vendor(String vendorID, String vendorName) {
    		super();
    		this.vendorID = vendorID;
    		this.vendorName = vendorName;
    	}
    	/**
    	 * 
    	 */
    	public Vendor() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	/**
    	 * @return the vendorID
    	 */
    	public String getVendorID() {
    		return vendorID;
    	}
    	/**
    	 * @param vendorID the vendorID to set
    	 */
    	public void setVendorID(String vendorID) {
    		this.vendorID = vendorID;
    	}
    	/**
    	 * @return the vendorName
    	 */
    	public String getVendorName() {
    		return vendorName;
    	}
    	/**
    	 * @param vendorName the vendorName to set
    	 */
    	public void setVendorName(String vendorName) {
    		this.vendorName = vendorName;
    	}
    	/* (non-Javadoc)
    	 * @see java.lang.Object#toString()
    	 */
    	@Override
    	public String toString() {
    		return "Vendor [vendorID=" + vendorID + ", vendorName=" + vendorName
    				+ "]";
    	}
    }
    

    public by HeathDernovich modified Aug 19, 2014  261714  0  4  1

    An example of inheriting from event emitter

    An example of inheriting from event emitter: EventEmitterPattern.js
    // Require our emitter
    
    var Emitter = require('events').EventEmitter;
    
    // Our main constructor
    
    var myEmitter = function (config) {
      // extend with emitter
      Emitter.call(this);
      };
      // Inherit from emitter, but keep my constructor
      
      myEmitter.prototype = Object.create(Emitter.prototype, {
        constructor: {
          value: myEmitter
        }
      });
      
      // methods
      
    myEmitter.prototype.setName = function (nam) {
      this.name = nam;
      this.emit('nameChanged', name);
     };
     
    module.exports = myEmitter;
    
    

    public by lbottaro modified Mar 23, 2014  293987  1  6  3

    Simple RMI Java client

    This is a simple RMI client implementation. Here the client program obtains a stub for the registry on the server's host, then looks up the remote object's stub by name in the registry. Finally invokes the sayHello method on the remote object using the stub. Here the client invokes the method exposes by Hello remote interface. Note that if no
    package example.hello;
    
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    
    public class Client {
    
        private Client() {}
    
        public static void main(String[] args) {
    
    	String host = (args.length < 1) ? null : args[0];
    	try {
    	    Registry registry = LocateRegistry.getRegistry(host);
    	    Hello stub = (Hello) registry.lookup("Hello");
    	    String response = stub.sayHello();
    	    System.out.println("response: " + response);
    	} catch (Exception e) {
    	    System.err.println("Client exception: " + e.toString());
    	    e.printStackTrace();
    	}
        }
    }    

    public by lbottaro modified Mar 23, 2014  281175  2  6  3

    Simple RMI Java server

    This code shows a basic RMI server example. A "server" is the class which has a main method that creates an instance of the remote object implementation, exports the remote object and then binds that instance to a name in a RMI registry. Here the server implements the remote interface Hello. The class that contains this main method could be the
    package example.hello;
    	
    import java.rmi.registry.Registry;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    	
    public class Server implements Hello {
    	
        public Server() {}
    
        public String sayHello() {
    	return "Hello, world!";
        }
    	
        public static void main(String args[]) {
    	
    	try {
    	    Server obj = new Server();
    	    Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
    
    	    // Bind the remote object's stub in the registry
    	    Registry registry = LocateRegistry.getRegistry();
    	    registry.bind("Hello", stub);
    
    	    System.err.println("Server ready");
    	} catch (Exception e) {
    	    System.err.println("Server exception: " + e.toString());
    	    e.printStackTrace();
    	}
        }
    }    

    public by yulian modified Nov 1, 2014  207438  3  5  0

    struct code example

    c
    typedef unsigned short byte2;
    typedef unsigned long  byte4;
    
    struct header
    {
        /* totally 44 bytes;        endianness (all the rest are little) */
        
        /* RIFF */
        char    ChunkID[4];         // big
        byte4   ChunkSize;
        char    Format[4];          // big
        /* fmt  */
        char    SubChunk1ID[4];     // big
        byte4   SubChunk1Size;
        byte2   AudioFormat;
        byte2   NumChannels;
        byte4   SampleRate;
        byte4   ByteRate;
        byte2   BlockAlign;
        byte2   BitsPerSample;
        /* data */
        char    SubChunk2ID[4];     // big
        byte4   SubChunk2Size;
    };
    • Public Snippets
    • Channels Snippets