Ron and Ella Wiki Page

Extremely Serious

Page 27 of 33

Reading a Batch Variable without the Quotes

Read a Batch Argument without the Quotes

This can be done by inserting a tilde (i.e. ~) character between the percent character and the corresponding argument position. 

For example for argument position 1 we can access it as the following:

%~1

Read a Batch Variable without the Quotes

This can be done with the following syntax:

%<VARIABLE_NAME>:"=%

Note: This is just using the replacement syntax as follows:

%<VARIABLE_NAME>:<FIND>=<REPLACE>%

Basic log4j.properties for console and file output

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Console output
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n

# File output
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%t - %m%n

Using Map to Code Conditionally without using the If statement

Preface

The following is an example of conditional programming using the key of the map as the condition and the value is the logic to be executed when a particular key was satisfied.

uses java.util.LinkedHashMap

var mainLogicIfRegistry : LinkedHashMap<block(___param : int) : boolean, block()>
 = {
  \ ___param : int -> 1 == ___param -> \ -> {print("one-0")},
  \ ___param : int -> 1 == ___param -> \ -> {print("one-1")},
  \ ___param : int -> 2 == ___param -> \ -> {print("two")},
  \ ___param : int -> 3 == ___param -> \ -> {print("three")}
}

(1..4).each(\ ___idx -> 
  mainLogicIfRegistry.Keys.where(\ ___cond -> ___cond(___idx))
    .each(\ ___key -> mainLogicIfRegistry.get(___key)())
)

We are using a LinkedHashMap in the example to make the execution based on the order of entries the logic were added in the map. If we are not that concern about the order of logic we can just use a regular map (i.e. HashMap).

Inspecting the First Entry in the Map

Let's see how the following first logic was registered in the map.

\ ___param : int -> 1 == ___param -> \ -> {print("one-0")}

The key part of the entry here is the following:

\ ___param : int -> 1 == ___param

The value part of the entry here is the following:

\ -> {print("one-0")}

As we can see both the key and value are lambda expressions and we can repeat the same lambda expression (i.e. but must be different instances) as the key (i.e. as long as you instantiate them separately) since those two are different instances of a block. Knowing this, we can filter the map based on the key (i.e. condition logic) and if it returns true use that key instance to retrieve the value (i.e. logic to execute) from the map.

Note: The lambda expression on the keys must have the same argument signatures. Because the key of the map must be of a consistent type.

Lets interpret the key part, all the lambda expressions of the key requires a parameter (i.e. for consistency of the block instance generated from the lambda expression). So it has one, and is being used to compare to 1. Actually both the first and second entries has the same lambda implementation but with different instances of block. Thus, when we pass in 1, both the first and the second entries will be executed.

Now for the value part, it is just printing the string one-0. To distinguish this from the second entry (i.e. since both the first and second entries has the same lambda expression), the second entry prints the string one-1.

Executing the Entries in the Map

Execute a particular entries using the following statement:

mainLogicIfRegistry.Keys.where(\ ___cond -> ___cond(___idx)).each(\ ___key -> mainLogicIfRegistry.get(___key)())

Lets dissect the statement above. We filter the key by executing the logic in the key like the following (i.e. ___idx is a argument of the lambda expression):

mainLogicIfRegistry.Keys.where(\ ___cond -> ___cond(___idx))

Once all the targeted keys were found execute each one like the following:

.each(\ ___key -> mainLogicIfRegistry.get(___key)())

If we execute the code from the preface it will be like this:

one-0
one-1
two
three

As we can see, both the 1 were executed (i.e. one-0 and one-1) followed by the 2 and 3. And since there's no entry for the 4 nothing was executed for it.

PoSH-GVM (PowerSHell Groovy enVironment Manager)

Source:  https://github.com/flofreud/posh-gvm

Introduction

A tool for managing parallel Versions of multiple Software Development Kits on a windows based system.

Powershell Behind Proxy
Setting default Powershell TLS Protocol

Installation via Short Script

Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/flofreud/posh-gvm/master/GetPoshGvm.ps1' | iex

Usage

Displaying the tool capability using the following:

gvm [help]

Useful Commands

Command Description
list Lists the available, installed and default versions of the candidate.
install Downloads and installs a particular candidate version (i.e. will default to latest if version is not specified)
use Uses a particular candidate version that is not the default.
default Sets a default version to use of a particular candidate.
current Displays the current active candidates version.
uninstall Uninstalls a particular candidate version.

Enabling Long Path Names

  1. Hit the windows key.
  2. Type gpedit.msc (i.e. Microsoft Common Console Document) and Run as administrator.
  3. Navigate to Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem.

  4. On the right pane double click Enable Win32 long paths.
  5. Select the Enabled radio button.

  6. Click the OK button.

J2EE Design Patterns

Design patterns are fundamental concepts in software engineering that provide reusable solutions to common problems. They help in creating more maintainable and scalable software. In this article, we will explore several design patterns and provide Java examples for each pattern's practical application.

Presentation Tier

Intercepting Filter Pattern

The Intercepting Filter Pattern is used when the intent is to perform pre or post-processing with the request or response of an application, typically in web applications. In Java, you can implement this pattern using servlet filters. Here's an example:

public class LoggingFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        // Pre-processing logic
        // ...

        // Pass the request to the next filter or servlet in the chain
        chain.doFilter(request, response);

        // Post-processing logic
        // ...
    }

    // Other methods for initialization and cleanup
}

Front Controller Pattern

The Front Controller Pattern is employed when the intent is to have a centralized request handling mechanism that dispatches requests to the appropriate handlers. In Java EE applications, a servlet can act as a front controller. Here's a simplified example:

public class FrontControllerServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        // Determine the appropriate handler for the request
        // ...

        // Dispatch to the appropriate handler
        // ...
    }
}

Business Tier

Business Delegate Pattern

The Business Delegate Pattern is used when the intent is to provide a single point of entry for clients to access business services, especially when separating the presentation tier and the business tier. Here's a simple Java example:

public class BusinessDelegate {
    private BusinessService businessService;

    public BusinessDelegate() {
        businessService = new BusinessService();
    }

    public void doTask() {
        // Delegate the task to the business service
        businessService.doTask();
    }
}

Composite Entity Pattern

The Composite Entity Pattern is often used in database applications to manage a graph of persistent objects using a primary composite entity. Here's a basic Java example:

public class CompositeEntity {
    private CoarseGrainedObject cgo = new CoarseGrainedObject();

    public void setData(String data1, String data2) {
        cgo.setData(data1, data2);
    }

    public String[] getData() {
        return cgo.getData();
    }
}

Service Locator Pattern

The Service Locator Pattern is employed when the intent is to locate services using JNDI (Java Naming and Directory Interface) lookup. Here's a simplified example:

public class ServiceLocator {
    public Service getService(String serviceName) {
        // Perform JNDI lookup to obtain the service
        // ...

        // Return the service
        return service;
    }
}

Session Facade Pattern

The Session Facade Pattern can be used in Java EE applications when the intent is to encapsulate business-tier components and expose a coarse-grained service to remote clients. Here's a basic example:

public class SessionFacade {
    private BusinessComponent businessComponent;

    public SessionFacade() {
        businessComponent = new BusinessComponent();
    }

    public void performBusinessOperation() {
        // Encapsulate business logic and provide a coarse-grained service
        businessComponent.performOperation();
    }
}

Transfer Object (Value Object) Pattern

The Transfer Object Pattern is used to pass data with multiple attributes in one shot from the client to the server. In Java, this pattern is typically implemented using POJOs (Plain Old Java Objects) with getter and setter methods, and the objects must be serializable. Here's a simple example:

public class TransferObject implements Serializable {
    private String attribute1;
    private int attribute2;

    // Getter and setter methods for attributes
    // ...
}

Value List Handler Pattern

The Value List Handler Pattern is used for managing the results of search operations, especially when results can be paged and traversed iteratively. Here's a basic example:

public class ValueListHandler {
    public List<ValueObject> getPage(int pageNumber) {
        // Retrieve a specific page of results
        // ...
    }
}

Integration Tier

Data Access Object (DAO) Pattern

The Data Access Object (DAO) Pattern is commonly used in Java EE applications to separate data persistence logic into a separate layer. Here's a simplified example:

public class UserDao {
    public User getUserById(int userId) {
        // Data access logic to retrieve a user from the database
        // ...
    }
}

Service Activator Pattern

The Service Activator Pattern is used to invoke a service asynchronously. An example of this pattern is a JMS (Java Message Service) listener that waits for a messaging request and delegates it to an appropriate service. Here's a high-level example:

public class ServiceActivator {
    public void activateService(ServiceRequest request) {
        // Asynchronously invoke the service based on the request
        // ...
    }
}

In conclusion, design patterns are invaluable tools for designing and architecting software systems. They provide tested and proven solutions to recurring design problems. By applying these design patterns in your Java applications, you can enhance code reusability, maintainability, and scalability, ultimately leading to more robust and efficient software.

Windows Command Output to Clipboard

Sometimes it is useful to get the output of a command and paste it to a text editor. For this we can use piping and the clip (i.e. sends the output to Windows clipboard) command.

Example:

If we go to any directory and run the tree command but it returns a very long output that exceeds the screen. We can redirect the output to Windows clipboard temporarily then paste it to a text editor.

  1. Open a command terminal.
  2. Try to run the following tree command.

%USERPROFILE%>tree

Where %USERPROFILE% is normally resolves to our local home directory.

The output might not fit to command terminal screen.

  1. Try redirecting it to a Windows clipboard with the following command:

%USERPROFILE%>tree | clip

You will see no output because it is directed to a Windows clipboard.

  1. Open a text editor of our choice (or anything where we wanted to paste the output).

Check if you can confirm the last entries from step 2 exists to our pasted output.

« Older posts Newer posts »